| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356 |
- <?php declare(strict_types=1);
- /*
- * 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\Utils;
- use Monolog\Formatter\NormalizerFormatter;
- 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
- {
- public const COLOR_DANGER = 'danger';
- public const COLOR_WARNING = 'warning';
- public const COLOR_GOOD = 'good';
- public const COLOR_DEFAULT = '#e3e4e6';
- /**
- * Slack channel (encoded ID or name)
- * @var string|null
- */
- private $channel;
- /**
- * Name of a bot
- * @var string|null
- */
- private $username;
- /**
- * User icon e.g. 'ghost', 'http://example.com/user.png'
- * @var string|null
- */
- private $userIcon;
- /**
- * 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;
- /**
- * Dot separated list of fields to exclude from slack message. E.g. ['context.field1', 'extra.field2']
- * @var array
- */
- private $excludeFields;
- /**
- * @var FormatterInterface
- */
- private $formatter;
- /**
- * @var NormalizerFormatter
- */
- private $normalizerFormatter;
- public function __construct(
- ?string $channel = null,
- ?string $username = null,
- bool $useAttachment = true,
- ?string $userIcon = null,
- bool $useShortAttachment = false,
- bool $includeContextAndExtra = false,
- array $excludeFields = array(),
- FormatterInterface $formatter = null
- ) {
- $this
- ->setChannel($channel)
- ->setUsername($username)
- ->useAttachment($useAttachment)
- ->setUserIcon($userIcon)
- ->useShortAttachment($useShortAttachment)
- ->includeContextAndExtra($includeContextAndExtra)
- ->excludeFields($excludeFields)
- ->setFormatter($formatter);
- if ($this->includeContextAndExtra) {
- $this->normalizerFormatter = new NormalizerFormatter();
- }
- }
- /**
- * Returns required data in format that Slack
- * is expecting.
- */
- public function getSlackData(array $record): array
- {
- $dataArray = array();
- $record = $this->removeExcludedFields($record);
- if ($this->username) {
- $dataArray['username'] = $this->username;
- }
- if ($this->channel) {
- $dataArray['channel'] = $this->channel;
- }
- if ($this->formatter && !$this->useAttachment) {
- $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(),
- 'mrkdwn_in' => array('fields'),
- 'ts' => $record['datetime']->getTimestamp(),
- );
- if ($this->useShortAttachment) {
- $attachment['title'] = $record['level_name'];
- } else {
- $attachment['title'] = 'Message';
- $attachment['fields'][] = $this->generateAttachmentField('Level', $record['level_name']);
- }
- if ($this->includeContextAndExtra) {
- foreach (array('extra', 'context') as $key) {
- if (empty($record[$key])) {
- continue;
- }
- if ($this->useShortAttachment) {
- $attachment['fields'][] = $this->generateAttachmentField(
- (string) $key,
- $record[$key]
- );
- } 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->userIcon) {
- if (filter_var($this->userIcon, FILTER_VALIDATE_URL)) {
- $dataArray['icon_url'] = $this->userIcon;
- } else {
- $dataArray['icon_emoji'] = ":{$this->userIcon}:";
- }
- }
- return $dataArray;
- }
- /**
- * Returns a Slack message attachment color associated with
- * provided level.
- */
- public function getAttachmentColor(int $level): string
- {
- switch (true) {
- case $level >= Logger::ERROR:
- return static::COLOR_DANGER;
- case $level >= Logger::WARNING:
- return static::COLOR_WARNING;
- case $level >= Logger::INFO:
- return static::COLOR_GOOD;
- default:
- return static::COLOR_DEFAULT;
- }
- }
- /**
- * Stringifies an array of key/value pairs to be used in attachment fields
- */
- public function stringify(array $fields): string
- {
- $normalized = $this->normalizerFormatter->format($fields);
- $hasSecondDimension = count(array_filter($normalized, 'is_array'));
- $hasNonNumericKeys = !count(array_filter(array_keys($normalized), 'is_numeric'));
- return $hasSecondDimension || $hasNonNumericKeys
- ? Utils::jsonEncode($normalized, JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE)
- : Utils::jsonEncode($normalized, JSON_UNESCAPED_UNICODE);
- }
- /**
- * Channel used by the bot when posting
- *
- * @param ?string $channel
- *
- * @return SlackHandler
- */
- public function setChannel(?string $channel = null): self
- {
- $this->channel = $channel;
- return $this;
- }
- /**
- * Username used by the bot when posting
- *
- * @param ?string $username
- *
- * @return SlackHandler
- */
- public function setUsername(?string $username = null): self
- {
- $this->username = $username;
- return $this;
- }
- public function useAttachment(bool $useAttachment = true): self
- {
- $this->useAttachment = $useAttachment;
- return $this;
- }
- public function setUserIcon(?string $userIcon = null): self
- {
- $this->userIcon = $userIcon;
- if (\is_string($userIcon)) {
- $this->userIcon = trim($userIcon, ':');
- }
- return $this;
- }
- public function useShortAttachment(bool $useShortAttachment = false): self
- {
- $this->useShortAttachment = $useShortAttachment;
- return $this;
- }
- public function includeContextAndExtra(bool $includeContextAndExtra = false): self
- {
- $this->includeContextAndExtra = $includeContextAndExtra;
- if ($this->includeContextAndExtra) {
- $this->normalizerFormatter = new NormalizerFormatter();
- }
- return $this;
- }
- public function excludeFields(array $excludeFields = []): self
- {
- $this->excludeFields = $excludeFields;
- return $this;
- }
- public function setFormatter(?FormatterInterface $formatter = null): self
- {
- $this->formatter = $formatter;
- return $this;
- }
- /**
- * Generates attachment field
- *
- * @param string|array $value
- */
- private function generateAttachmentField(string $title, $value): array
- {
- $value = is_array($value)
- ? sprintf('```%s```', substr($this->stringify($value), 0, 1990))
- : $value;
- return array(
- 'title' => ucfirst($title),
- 'value' => $value,
- 'short' => false,
- );
- }
- /**
- * Generates a collection of attachment fields from array
- */
- private function generateAttachmentFields(array $data): array
- {
- $fields = array();
- foreach ($this->normalizerFormatter->format($data) as $key => $value) {
- $fields[] = $this->generateAttachmentField((string) $key, $value);
- }
- return $fields;
- }
- /**
- * Get a copy of record with fields excluded according to $this->excludeFields
- */
- private function removeExcludedFields(array $record): array
- {
- foreach ($this->excludeFields as $field) {
- $keys = explode('.', $field);
- $node = &$record;
- $lastKey = end($keys);
- foreach ($keys as $key) {
- if (!isset($node[$key])) {
- break;
- }
- if ($lastKey === $key) {
- unset($node[$key]);
- break;
- }
- $node = &$node[$key];
- }
- }
- return $record;
- }
- }
|