| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248 |
- <?php
- /*
- * This file is part of the Monolog package.
- *
- * (c) Jordi Boggiano <j.boggiano@seld.be>
- *
- * For the full copyright and license information, please view the LICENSE
- * file that was distributed with this source code.
- */
- namespace Monolog\Handler\Slack;
- use Monolog\Logger;
- use Monolog\Formatter\LineFormatter;
- use Monolog\Formatter\FormatterInterface;
- /**
- * Slack record utility helping to log to Slack webhooks or API.
- *
- * @author Greg Kedzierski <greg@gregkedzierski.com>
- * @author Haralan Dobrev <hkdobrev@gmail.com>
- * @see https://api.slack.com/incoming-webhooks
- * @see https://api.slack.com/docs/message-attachments
- */
- class SlackRecord
- {
- const COLOR_DANGER = 'danger';
- const COLOR_WARNING = 'warning';
- const COLOR_GOOD = 'good';
- const COLOR_DEFAULT = '#e3e4e6';
- /**
- * Slack channel (encoded ID or name)
- * @var string|null
- */
- private $channel;
- /**
- * Name of a bot
- * @var string
- */
- private $username;
- /**
- * Emoji icon name
- * @var string
- */
- private $iconEmoji;
- /**
- * Whether the message should be added to Slack as attachment (plain text otherwise)
- * @var bool
- */
- private $useAttachment;
- /**
- * Whether the the context/extra messages added to Slack as attachments are in a short style
- * @var bool
- */
- private $useShortAttachment;
- /**
- * Whether the attachment should include context and extra data
- * @var bool
- */
- private $includeContextAndExtra;
- /**
- * @var FormatterInterface
- */
- private $formatter;
- /**
- * @var LineFormatter
- */
- private $lineFormatter;
- public function __construct($channel = null, $username = 'Monolog', $useAttachment = true, $iconEmoji = null, $useShortAttachment = false, $includeContextAndExtra = false, FormatterInterface $formatter = null)
- {
- $this->channel = $channel;
- $this->username = $username;
- $this->iconEmoji = trim($iconEmoji, ':');
- $this->useAttachment = $useAttachment;
- $this->useShortAttachment = $useShortAttachment;
- $this->includeContextAndExtra = $includeContextAndExtra;
- $this->formatter = $formatter;
- if ($this->includeContextAndExtra) {
- $this->lineFormatter = new LineFormatter();
- }
- }
- public function getSlackData(array $record)
- {
- $dataArray = array(
- 'username' => $this->username,
- 'text' => '',
- );
- if ($this->channel) {
- $dataArray['channel'] = $this->channel;
- }
- if ($this->formatter) {
- $message = $this->formatter->format($record);
- } else {
- $message = $record['message'];
- }
- if ($this->useAttachment) {
- $attachment = array(
- 'fallback' => $message,
- 'text' => $message,
- 'color' => $this->getAttachmentColor($record['level']),
- 'fields' => array(),
- );
- if ($this->useShortAttachment) {
- $attachment['title'] = $record['level_name'];
- } else {
- $attachment['title'] = 'Message';
- $attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name'], true);
- }
- if ($this->includeContextAndExtra) {
- foreach (array('extra', 'context') as $key) {
- if (empty($record[$key])) {
- continue;
- }
- if ($this->useShortAttachment) {
- $attachment['fields'][] = $this->generateAttachmentField(
- ucfirst($key),
- $this->stringify($record[$key]),
- true
- );
- } else {
- // Add all extra fields as individual fields in attachment
- $attachment['fields'] = array_merge(
- $attachment['fields'],
- $this->generateAttachmentFields($record[$key])
- );
- }
- }
- }
- $dataArray['attachments'] = array($attachment);
- } else {
- $dataArray['text'] = $message;
- }
- if ($this->iconEmoji) {
- $dataArray['icon_emoji'] = ":{$this->iconEmoji}:";
- }
- return $dataArray;
- }
- /**
- * Returned a Slack message attachment color associated with
- * provided level.
- *
- * @param int $level
- * @return string
- */
- public function getAttachmentColor($level)
- {
- switch (true) {
- case $level >= Logger::ERROR:
- return self::COLOR_DANGER;
- case $level >= Logger::WARNING:
- return self::COLOR_WARNING;
- case $level >= Logger::INFO:
- return self::COLOR_GOOD;
- default:
- return self::COLOR_DEFAULT;
- }
- }
- /**
- * Stringifies an array of key/value pairs to be used in attachment fields
- *
- * @param array $fields
- * @return string|null
- */
- public function stringify($fields)
- {
- if (!$this->lineFormatter) {
- return null;
- }
- $string = '';
- foreach ($fields as $var => $val) {
- $string .= $var.': '.$this->lineFormatter->stringify($val)." | ";
- }
- $string = rtrim($string, " |");
- return $string;
- }
- /**
- * Sets the formatter
- *
- * @param FormatterInterface $formatter
- */
- public function setFormatter(FormatterInterface $formatter)
- {
- $this->formatter = $formatter;
- }
- /**
- * Generates attachment field
- *
- * @param string $title
- * @param string|array $value
- * @param bool $short
- * @return array
- */
- private function generateAttachmentField($title, $value, $short)
- {
- return array(
- 'title' => $title,
- 'value' => is_array($value) ? $this->lineFormatter->stringify($value) : $value,
- 'short' => $short
- );
- }
- /**
- * Generates a collection of attachment fields from array
- *
- * @param array $data
- * @return array
- */
- private function generateAttachmentFields(array $data)
- {
- $fields = array();
- foreach ($data as $key => $value) {
- $fields[] = $this->generateAttachmentField($key, $value, false);
- }
- return $fields;
- }
- }
|