Skip to content
Snippets Groups Projects
Commit 4764557b authored by Chris Hallberg's avatar Chris Hallberg Committed by Demian Katz
Browse files

Slack Logger Visual Improvements (#790)

- Icons at the start of messages.
- Username configuration.
parent c02a515b
Branches
Tags
No related merge requests found
...@@ -1073,6 +1073,7 @@ skip_numeric = true ...@@ -1073,6 +1073,7 @@ skip_numeric = true
; Get URL from https://YOURSLACK.slack.com/apps/manage/custom-integrations ; Get URL from https://YOURSLACK.slack.com/apps/manage/custom-integrations
;slack = #channel_name:alert,error ;slack = #channel_name:alert,error
;slackurl = https://hooks.slack.com/services/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 ;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 ; This section can be used to specify a "parent configuration" from which
; the current configuration file will inherit. You can chain multiple ; the current configuration file will inherit. You can chain multiple
......
...@@ -121,21 +121,30 @@ class Logger extends BaseLogger implements ServiceLocatorAwareInterface ...@@ -121,21 +121,30 @@ class Logger extends BaseLogger implements ServiceLocatorAwareInterface
// Activate slack logging, if applicable: // Activate slack logging, if applicable:
if (isset($config->Logging->slack) && isset($config->Logging->slackurl)) { if (isset($config->Logging->slack) && isset($config->Logging->slackurl)) {
$options = [];
// Get config // Get config
list($channel, $error_types) = explode(':', $config->Logging->slack); list($channel, $error_types) = explode(':', $config->Logging->slack);
if ($error_types == null) { if ($error_types == null) {
$error_types = $channel; $error_types = $channel;
$channel = null; $channel = null;
} }
if ($channel) {
$options['channel'] = $channel;
}
if (isset($config->Logging->slackname)) {
$options['name'] = $config->Logging->slackname;
}
$filters = explode(',', $error_types); $filters = explode(',', $error_types);
// Make Writers // Make Writers
$writer = new Writer\Slack( $writer = new Writer\Slack(
$config->Logging->slackurl, $config->Logging->slackurl,
$this->getServiceLocator()->get('VuFind\Http')->createClient(), $this->getServiceLocator()->get('VuFind\Http')->createClient(),
$channel $options
); );
$writer->setContentType('application/json'); $writer->setContentType('application/json');
$formatter = new \Zend\Log\Formatter\Simple("%priorityName%: %message%"); $formatter = new \Zend\Log\Formatter\Simple(
"*%priorityName%*: %message%"
);
$writer->setFormatter($formatter); $writer->setFormatter($formatter);
$this->addWriters($writer, $filters); $this->addWriters($writer, $filters);
} }
......
...@@ -44,18 +44,47 @@ class Slack extends Post ...@@ -44,18 +44,47 @@ class Slack extends Post
* *
* @var string * @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 * Constructor
* *
* @param string $url URL to open as a stream * @param string $url URL to open as a stream
* @param Client $client Pre-configured http client * @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); parent::__construct($url, $client);
} }
...@@ -68,10 +97,12 @@ class Slack extends Post ...@@ -68,10 +97,12 @@ class Slack extends Post
*/ */
protected function getBody($event) protected function getBody($event)
{ {
$data = ['text' => $this->formatter->format($event) . PHP_EOL]; $data = [
if ($this->channel) { '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); return json_encode($data);
} }
} }
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment