FleepHookHandler.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php
  2. /*
  3. * This file is part of the Monolog package.
  4. *
  5. * (c) Jordi Boggiano <j.boggiano@seld.be>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Monolog\Handler;
  11. use Monolog\Formatter\LineFormatter;
  12. use Monolog\Logger;
  13. /**
  14. * Sends logs to Fleep.io using Webhook integrations
  15. *
  16. * You'll need a Fleep.io account to use this handler.
  17. *
  18. * @see https://fleep.io/integrations/webhooks/ Fleep Webhooks Documentation
  19. * @author Ando Roots <ando@sqroot.eu>
  20. */
  21. class FleepHookHandler extends SocketHandler
  22. {
  23. const FLEEP_HOST = 'fleep.io';
  24. const FLEEP_HOOK_URI = '/hook/';
  25. /**
  26. * @var string Webhook token (specifies the conversation where logs are sent)
  27. */
  28. protected $token;
  29. /**
  30. * Construct a new Fleep.io Handler.
  31. *
  32. * For instructions on how to create a new web hook in your conversations
  33. * see https://fleep.io/integrations/webhooks/
  34. *
  35. * @param string $token Webhook token
  36. * @param bool|int $level The minimum logging level at which this handler will be triggered
  37. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  38. * @throws MissingExtensionException
  39. */
  40. public function __construct($token, $level = Logger::DEBUG, $bubble = true)
  41. {
  42. if (!extension_loaded('openssl')) {
  43. throw new MissingExtensionException('The OpenSSL PHP extension is required to use the FleepHookHandler');
  44. }
  45. $this->token = $token;
  46. $connectionString = 'ssl://' . self::FLEEP_HOST . ':443';
  47. parent::__construct($connectionString, $level, $bubble);
  48. }
  49. /**
  50. * Returns the default formatter to use with this handler
  51. *
  52. * Overloaded to remove empty context and extra arrays from the end of the log message.
  53. *
  54. * @return LineFormatter
  55. */
  56. protected function getDefaultFormatter()
  57. {
  58. return new LineFormatter(null, null, true, true);
  59. }
  60. /**
  61. * Handles a log record
  62. *
  63. * @param array $record
  64. */
  65. public function write(array $record)
  66. {
  67. parent::write($record);
  68. $this->closeSocket();
  69. }
  70. /**
  71. * {@inheritdoc}
  72. *
  73. * @param array $record
  74. * @return string
  75. */
  76. protected function generateDataStream($record)
  77. {
  78. $content = $this->buildContent($record);
  79. return $this->buildHeader($content) . $content;
  80. }
  81. /**
  82. * Builds the header of the API Call
  83. *
  84. * @param string $content
  85. * @return string
  86. */
  87. private function buildHeader($content)
  88. {
  89. $header = "POST " . self::FLEEP_HOOK_URI . $this->token . " HTTP/1.1\r\n";
  90. $header .= "Host: " . self::FLEEP_HOST . "\r\n";
  91. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  92. $header .= "Content-Length: " . strlen($content) . "\r\n";
  93. $header .= "\r\n";
  94. return $header;
  95. }
  96. /**
  97. * Builds the body of API call
  98. *
  99. * @param array $record
  100. * @return string
  101. */
  102. private function buildContent($record)
  103. {
  104. $dataArray = array(
  105. 'message' => $record['formatted'],
  106. );
  107. return http_build_query($dataArray);
  108. }
  109. }