SlackWebhookHandler.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  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\Level;
  13. use Monolog\Utils;
  14. use Monolog\Handler\Slack\SlackRecord;
  15. use Monolog\LogRecord;
  16. /**
  17. * Sends notifications through Slack Webhooks
  18. *
  19. * @author Haralan Dobrev <hkdobrev@gmail.com>
  20. * @see https://api.slack.com/incoming-webhooks
  21. */
  22. class SlackWebhookHandler extends AbstractProcessingHandler
  23. {
  24. /**
  25. * Slack Webhook token
  26. */
  27. private string $webhookUrl;
  28. /**
  29. * Instance of the SlackRecord util class preparing data for Slack API.
  30. */
  31. private SlackRecord $slackRecord;
  32. /**
  33. * @param string $webhookUrl Slack Webhook URL
  34. * @param string|null $channel Slack channel (encoded ID or name)
  35. * @param string|null $username Name of a bot
  36. * @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise)
  37. * @param string|null $iconEmoji The emoji name to use (or null)
  38. * @param bool $useShortAttachment Whether the the context/extra messages added to Slack as attachments are in a short style
  39. * @param bool $includeContextAndExtra Whether the attachment should include context and extra data
  40. * @param string[] $excludeFields Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2']
  41. *
  42. * @throws MissingExtensionException If the curl extension is missing
  43. */
  44. public function __construct(
  45. string $webhookUrl,
  46. ?string $channel = null,
  47. ?string $username = null,
  48. bool $useAttachment = true,
  49. ?string $iconEmoji = null,
  50. bool $useShortAttachment = false,
  51. bool $includeContextAndExtra = false,
  52. $level = Level::Critical,
  53. bool $bubble = true,
  54. array $excludeFields = []
  55. ) {
  56. if (!\extension_loaded('curl')) {
  57. throw new MissingExtensionException('The curl extension is needed to use the SlackWebhookHandler');
  58. }
  59. parent::__construct($level, $bubble);
  60. $this->webhookUrl = $webhookUrl;
  61. $this->slackRecord = new SlackRecord(
  62. $channel,
  63. $username,
  64. $useAttachment,
  65. $iconEmoji,
  66. $useShortAttachment,
  67. $includeContextAndExtra,
  68. $excludeFields
  69. );
  70. }
  71. public function getSlackRecord(): SlackRecord
  72. {
  73. return $this->slackRecord;
  74. }
  75. public function getWebhookUrl(): string
  76. {
  77. return $this->webhookUrl;
  78. }
  79. /**
  80. * @inheritDoc
  81. */
  82. protected function write(LogRecord $record): void
  83. {
  84. $postData = $this->slackRecord->getSlackData($record);
  85. $postString = Utils::jsonEncode($postData);
  86. $ch = curl_init();
  87. $options = [
  88. CURLOPT_URL => $this->webhookUrl,
  89. CURLOPT_POST => true,
  90. CURLOPT_RETURNTRANSFER => true,
  91. CURLOPT_HTTPHEADER => ['Content-type: application/json'],
  92. CURLOPT_POSTFIELDS => $postString,
  93. ];
  94. curl_setopt_array($ch, $options);
  95. Curl\Util::execute($ch);
  96. }
  97. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  98. {
  99. parent::setFormatter($formatter);
  100. $this->slackRecord->setFormatter($formatter);
  101. return $this;
  102. }
  103. public function getFormatter(): FormatterInterface
  104. {
  105. $formatter = parent::getFormatter();
  106. $this->slackRecord->setFormatter($formatter);
  107. return $formatter;
  108. }
  109. }