SlackWebhookHandler.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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\Logger;
  13. use Monolog\Handler\Slack\SlackRecord;
  14. /**
  15. * Sends notifications through Slack Webhooks
  16. *
  17. * @author Haralan Dobrev <hkdobrev@gmail.com>
  18. * @see https://api.slack.com/incoming-webhooks
  19. */
  20. class SlackWebhookHandler extends AbstractProcessingHandler
  21. {
  22. /**
  23. * Slack Webhook token
  24. * @var string
  25. */
  26. private $webhookUrl;
  27. /**
  28. * Instance of the SlackRecord util class preparing data for Slack API.
  29. * @var SlackRecord
  30. */
  31. private $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|int $level The minimum logging level at which this handler will be triggered
  41. * @param bool $bubble Whether the messages that are handled can bubble up the stack or not
  42. * @param array $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 = Logger::CRITICAL,
  53. bool $bubble = true,
  54. array $excludeFields = array()
  55. ) {
  56. parent::__construct($level, $bubble);
  57. $this->webhookUrl = $webhookUrl;
  58. $this->slackRecord = new SlackRecord(
  59. $channel,
  60. $username,
  61. $useAttachment,
  62. $iconEmoji,
  63. $useShortAttachment,
  64. $includeContextAndExtra,
  65. $excludeFields
  66. );
  67. }
  68. public function getSlackRecord(): SlackRecord
  69. {
  70. return $this->slackRecord;
  71. }
  72. public function getWebhookUrl(): string
  73. {
  74. return $this->webhookUrl;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. *
  79. * @param array $record
  80. */
  81. protected function write(array $record): void
  82. {
  83. $postData = $this->slackRecord->getSlackData($record);
  84. $postString = json_encode($postData);
  85. $ch = curl_init();
  86. $options = array(
  87. CURLOPT_URL => $this->webhookUrl,
  88. CURLOPT_POST => true,
  89. CURLOPT_RETURNTRANSFER => true,
  90. CURLOPT_HTTPHEADER => array('Content-type: application/json'),
  91. CURLOPT_POSTFIELDS => $postString,
  92. );
  93. if (defined('CURLOPT_SAFE_UPLOAD')) {
  94. $options[CURLOPT_SAFE_UPLOAD] = true;
  95. }
  96. curl_setopt_array($ch, $options);
  97. Curl\Util::execute($ch);
  98. }
  99. public function setFormatter(FormatterInterface $formatter): HandlerInterface
  100. {
  101. parent::setFormatter($formatter);
  102. $this->slackRecord->setFormatter($formatter);
  103. return $this;
  104. }
  105. public function getFormatter(): FormatterInterface
  106. {
  107. $formatter = parent::getFormatter();
  108. $this->slackRecord->setFormatter($formatter);
  109. return $formatter;
  110. }
  111. }