FleepHookHandler.php 3.4 KB

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