diff --git a/config/vufind/config.ini b/config/vufind/config.ini index 4d36bb5b9402a4d3a9a5725e8a41b2a230ef5e08..53e74537f5a16151b948e74c0a500d35057f86fb 100644 --- a/config/vufind/config.ini +++ b/config/vufind/config.ini @@ -1073,6 +1073,7 @@ skip_numeric = true ; Get URL from https://YOURSLACK.slack.com/apps/manage/custom-integrations ;slack = #channel_name:alert,error ;slackurl = https://hooks.slack.com/services/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 +;slackname = "VuFind Log" ; username messages are posted under ; This section can be used to specify a "parent configuration" from which ; the current configuration file will inherit. You can chain multiple diff --git a/module/VuFind/src/VuFind/Log/Logger.php b/module/VuFind/src/VuFind/Log/Logger.php index f1817322b6ab910a137e17b4e09e5dbed6493c5a..9b97e58e1f4b76d43a7ad4c378477f5055cb8615 100644 --- a/module/VuFind/src/VuFind/Log/Logger.php +++ b/module/VuFind/src/VuFind/Log/Logger.php @@ -121,21 +121,30 @@ class Logger extends BaseLogger implements ServiceLocatorAwareInterface // Activate slack logging, if applicable: if (isset($config->Logging->slack) && isset($config->Logging->slackurl)) { + $options = []; // Get config list($channel, $error_types) = explode(':', $config->Logging->slack); if ($error_types == null) { $error_types = $channel; $channel = null; } + if ($channel) { + $options['channel'] = $channel; + } + if (isset($config->Logging->slackname)) { + $options['name'] = $config->Logging->slackname; + } $filters = explode(',', $error_types); // Make Writers $writer = new Writer\Slack( $config->Logging->slackurl, $this->getServiceLocator()->get('VuFind\Http')->createClient(), - $channel + $options ); $writer->setContentType('application/json'); - $formatter = new \Zend\Log\Formatter\Simple("%priorityName%: %message%"); + $formatter = new \Zend\Log\Formatter\Simple( + "*%priorityName%*: %message%" + ); $writer->setFormatter($formatter); $this->addWriters($writer, $filters); } diff --git a/module/VuFind/src/VuFind/Log/Writer/Slack.php b/module/VuFind/src/VuFind/Log/Writer/Slack.php index 06778455ee4f1a057c26303e12711e5f14d97755..26284522ac531f036b80f59d411c7afa74fa0e6f 100644 --- a/module/VuFind/src/VuFind/Log/Writer/Slack.php +++ b/module/VuFind/src/VuFind/Log/Writer/Slack.php @@ -44,18 +44,47 @@ class Slack extends Post * * @var string */ - protected $channel; + protected $channel = '#vufind_log'; + + /** + * The slack username messages are posted under. + * + * @var string + */ + protected $username = 'VuFind Log'; + + /** + * Icons that appear at the start of log messages in Slack, by severity + * + * @var array + */ + protected $messageIcons = [ + ':fire: :fire: :fire: ', // EMERG + ':rotating_light: ', // ALERT + ':red_circle: ', // CRIT + ':exclamation: ', // ERR + ':warning: ', // WARN + ':speech_balloon: ', // NOTICE + ':information_source: ', // INFO + ':beetle: ' // DEBUG + ]; /** * Constructor * * @param string $url URL to open as a stream * @param Client $client Pre-configured http client - * @param string $channel Slack channel + * @param array $options Optional settings (may contain 'channel' for the + * Slack channel to use and/or 'name' for the username messages are posted under) */ - public function __construct($url, Client $client, $channel = '#vufind_log') + public function __construct($url, Client $client, array $options = []) { - $this->channel = $channel; + if (isset($options['channel'])) { + $this->channel = $options['channel']; + } + if (isset($options['name'])) { + $this->username = $options['name']; + } parent::__construct($url, $client); } @@ -68,10 +97,12 @@ class Slack extends Post */ protected function getBody($event) { - $data = ['text' => $this->formatter->format($event) . PHP_EOL]; - if ($this->channel) { - $data['channel'] = $this->channel; - } + $data = [ + 'channel' => $this->channel, + 'username' => $this->username, + 'text' => $this->messageIcons[$event['priority']] + . $this->formatter->format($event) . PHP_EOL + ]; return json_encode($data); } }