SlackWebhookHandler.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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. * @var string
  27. */
  28. private $webhookUrl;
  29. /**
  30. * Instance of the SlackRecord util class preparing data for Slack API.
  31. * @var SlackRecord
  32. */
  33. private $slackRecord;
  34. /**
  35. * @param string $webhookUrl Slack Webhook URL
  36. * @param string|null $channel Slack channel (encoded ID or name)
  37. * @param string|null $username Name of a bot
  38. * @param bool $useAttachment Whether the message should be added to Slack as attachment (plain text otherwise)
  39. * @param string|null $iconEmoji The emoji name to use (or null)
  40. * @param bool $useShortAttachment Whether the the context/extra messages added to Slack as attachments are in a short style
  41. * @param bool $includeContextAndExtra Whether the attachment should include context and extra data
  42. * @param string[] $excludeFields Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2']
  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. if (defined('CURLOPT_SAFE_UPLOAD')) {
  95. $options[CURLOPT_SAFE_UPLOAD] = true;
  96. }
  97. curl_setopt_array($ch, $options);
  98. Curl\Util::execute($ch);
  99. }
  100. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  101. {
  102. parent::setFormatter($formatter);
  103. $this->slackRecord->setFormatter($formatter);
  104. return $this;
  105. }
  106. public function getFormatter(): FormatterInterface
  107. {
  108. $formatter = parent::getFormatter();
  109. $this->slackRecord->setFormatter($formatter);
  110. return $formatter;
  111. }
  112. }