cytec / laminas-log-slack
Write laminas-log messages to a slack channel
Installs: 3 221
Dependents: 0
Suggesters: 0
Security: 0
Stars: 8
Watchers: 5
Forks: 0
Open Issues: 0
Requires
- php: >=7.4
- laminas/laminas-http: ^2.14
- laminas/laminas-log: ^2.13
Requires (Dev)
- phpunit/phpunit: ^9.0
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:
- Open the channel you would like to receive your log in.
- Click on the channel name in the top left corner and from the drop down menu select "Add an app or integration"
- Click "Build" (top right)
- Click "Make a custom integration"
- Choose "Incoming WebHooks"
- 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.