FleepHookHandler.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  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. protected const FLEEP_HOST = 'fleep.io';
  25. protected 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 string|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(string $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://' . static::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. public function write(array $record): void
  65. {
  66. parent::write($record);
  67. $this->closeSocket();
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function generateDataStream(array $record): string
  73. {
  74. $content = $this->buildContent($record);
  75. return $this->buildHeader($content) . $content;
  76. }
  77. /**
  78. * Builds the header of the API Call
  79. */
  80. private function buildHeader(string $content): string
  81. {
  82. $header = "POST " . static::FLEEP_HOOK_URI . $this->token . " HTTP/1.1\r\n";
  83. $header .= "Host: " . static::FLEEP_HOST . "\r\n";
  84. $header .= "Content-Type: application/x-www-form-urlencoded\r\n";
  85. $header .= "Content-Length: " . strlen($content) . "\r\n";
  86. $header .= "\r\n";
  87. return $header;
  88. }
  89. /**
  90. * Builds the body of API call
  91. */
  92. private function buildContent(array $record): string
  93. {
  94. $dataArray = [
  95. 'message' => $record['formatted'],
  96. ];
  97. return http_build_query($dataArray);
  98. }
  99. }