cytec/laminas-log-slack

Write laminas-log messages to a slack channel

3.1.0 2021-07-21 11:05 UTC

This package is auto-updated.

Last update: 2024-11-21 17:56:35 UTC


README

Write laminas-log messages to a slack channel

Installation

composer require cytec/laminas-log-slack

Slack Configuration

The log writer works by sending information to a "Incoming WebHooks" integration in Slack:

  1. Open the channel you would like to receive your log in.
  2. Click on the channel name in the top left corner and from the drop down menu select "Add an app or integration"
  3. Click "Build" (top right)
  4. Click "Make a custom integration"
  5. Choose "Incoming WebHooks"
  6. Click the large green "Add Incoming WebHooks integration"

You'll need the Webhook URL to configure the writer. Under "Integration Settings" you can customize some defaults.

Usage

Manual

$writer = new \Cytec\Log\Writer\Slack('<YOUR_SLACK_WEBHOOK_URL>');

//Optional - use this filter only if you want to send critical messages to Slack
$writer->addFilter(new \Laminas\Log\Filter\Priority(\Laminas\Log\Logger::CRIT));

$logger = new \Laminas\Log\Logger();
$logger->addWriter($writer);

$logger->info('Informational message');
$logger->crit('Critical message');

//second "extra" parameter is supported and printed as properties in slack
$logger->crit('Critical message', $_SERVER);

Via Service manager

Somewhere in your configuration (eg. config/autoload/global.php) add

...
'log' => [
    'SlackLog' => [
        'writers' => [
            'default' => [
                'name' => 'Cytec\Log\Writer\Slack',
                'options' => [
                    'webhook_url' => '<YOUR_SLACK_WEBHOOK_URL>',
                    'bot_name' => 'Project Name',   //optional
                    'channel_override' => '#alerts',//optional @person is also supported
                    'filters' => \Laminas\Log\Logger::CRIT,      //optional - filter by priority
                ]
            ]
        ]
    ]
],
...

And then you can get the logger via the service manager:

$log = $this->getServiceManager()->get('SlackLog');
$log->crit('Critical message');

Keep in mind

This writer isn't suitable for constant writing due to the fact that log messages are sent via http interface to the Slack api. This makes it very slow and can add significant delay in application response times. The intended use is for critical or very high priority messages that need the immediate attention of a real person.