SlackWebhookHandler.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  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 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($webhookUrl, $channel = null, $username = null, $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeContextAndExtra = false, $level = Logger::CRITICAL, $bubble = true, array $excludeFields = array())
  45. {
  46. parent::__construct($level, $bubble);
  47. $this->webhookUrl = $webhookUrl;
  48. $this->slackRecord = new SlackRecord(
  49. $channel,
  50. $username,
  51. $useAttachment,
  52. $iconEmoji,
  53. $useShortAttachment,
  54. $includeContextAndExtra,
  55. $excludeFields,
  56. $this->formatter
  57. );
  58. }
  59. public function getSlackRecord()
  60. {
  61. return $this->slackRecord;
  62. }
  63. public function getWebhookUrl()
  64. {
  65. return $this->webhookUrl;
  66. }
  67. /**
  68. * {@inheritdoc}
  69. *
  70. * @param array $record
  71. */
  72. protected function write(array $record)
  73. {
  74. $postData = $this->slackRecord->getSlackData($record);
  75. $postString = json_encode($postData);
  76. $ch = curl_init();
  77. $options = array(
  78. CURLOPT_URL => $this->webhookUrl,
  79. CURLOPT_POST => true,
  80. CURLOPT_RETURNTRANSFER => true,
  81. CURLOPT_HTTPHEADER => array('Content-type: application/json'),
  82. CURLOPT_POSTFIELDS => $postString
  83. );
  84. if (defined('CURLOPT_SAFE_UPLOAD')) {
  85. $options[CURLOPT_SAFE_UPLOAD] = true;
  86. }
  87. curl_setopt_array($ch, $options);
  88. Curl\Util::execute($ch);
  89. }
  90. public function setFormatter(FormatterInterface $formatter)
  91. {
  92. parent::setFormatter($formatter);
  93. $this->slackRecord->setFormatter($formatter);
  94. return $this;
  95. }
  96. public function getFormatter()
  97. {
  98. $formatter = parent::getFormatter();
  99. $this->slackRecord->setFormatter($formatter);
  100. return $formatter;
  101. }
  102. }