Send exceptions and dump variables to Slack.
use Stasadev\SlackNotifier\Facades\SlackNotifier;
SlackNotifier::send(new \RuntimeException('Test exception'));
SlackNotifier::send('Test message');
Install the package via composer:
composer require stasadev/laravel-slack-notifier
All env variables used by this package (only LOG_SLACK_WEBHOOK_URL
is required):
APP_NAME=Laravel
LOG_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/ABC
LOG_SLACK_CHANNEL=
LOG_SLACK_EMOJI=:boom:
LOG_SLACK_CACHE_SECONDS=0
How to get a webhook URL in the Slack API docs.
To temporarily disable all logging, simply comment out LOG_SLACK_WEBHOOK_URL
or set it to an empty string or null
.
Optionally publish the config file with:
php artisan vendor:publish --tag="slack-notifier"
To send a message to Slack, simply call SlackNotifier::send()
.
// In Laravel 11.x and later
// bootstrap/app.php
return Application::configure(basePath: dirname(__DIR__))
->withExceptions(function (Exceptions $exceptions) {
$exceptions->reportable(function (Throwable $e) {
\Stasadev\SlackNotifier\Facades\SlackNotifier::send($e);
});
})->create();
// In Laravel 8.x, 9.x, 10.x
// app/Exceptions/Handler.php
public function register(): void
{
$this->reportable(function (Throwable $e) {
\Stasadev\SlackNotifier\Facades\SlackNotifier::send($e);
});
}
// In Laravel 7.x
// app/Exceptions/Handler.php
public function report(Throwable $exception)
{
if ($this->shouldReport($exception)) {
\Stasadev\SlackNotifier\Facades\SlackNotifier::send($exception);
}
parent::report($exception);
}
// In Laravel 5.7.x, 5.8.x, 6.x
// app/Exceptions/Handler.php
public function report(Exception $exception)
{
if ($this->shouldReport($exception)) {
\Stasadev\SlackNotifier\Facades\SlackNotifier::send($exception);
}
parent::report($exception);
}
use Stasadev\SlackNotifier\Facades\SlackNotifier;
$variable = 'message';
// $variable = ['test' => 'array'];
// $variable = new stdClass();
SlackNotifier::send($variable);
Use an alternative webhook, by specify extra ones in the config file.
// config/slack-notifier.php
'webhook_urls' => [
'default' => 'https://hooks.slack.com/services/ABC',
'testing' => 'https://hooks.slack.com/services/DEF',
],
The webhook to be used can be chosen using the to
function.
use Stasadev\SlackNotifier\Facades\SlackNotifier;
SlackNotifier::to('testing')->send('Test message');
The to
function also supports custom webhook URLs.
use Stasadev\SlackNotifier\Facades\SlackNotifier;
SlackNotifier::to('https://custom-url.com')->send('Test message');
You can send a message to a channel (use LOG_SLACK_CHANNEL
) other than the default one for the webhook, by passing it to the channel
function.
use Stasadev\SlackNotifier\Facades\SlackNotifier;
SlackNotifier::channel('reminders')->send('Test message');
Use username
(use APP_NAME
) and emoji
(use LOG_SLACK_EMOJI
) to make your messages unique, or override them right before sending.
use Stasadev\SlackNotifier\Facades\SlackNotifier;
SlackNotifier::username('My Laravel Bot')->emoji(':tada:')->send('Test message');
Extend the default Stasadev\SlackNotifier\SlackNotifierFormatter::class
to format the messages however you like. Then simply replace the formatter
key in the configuration file.
// config/slack-notifier.php
'formatter' => App\Formatters\CustomSlackNotifierFormatter::class,
Include additional context
in a Slack message (use dont_flash
to exclude sensitive info from context
). It will be added as an attachment.
Stack traces for exceptions in Laravel usually contain many lines, including framework files. Usually, you are only interested in tracking exception details in the application files.
You can filter it out with the dont_trace
config option.
Sometimes a large group of exceptions is thrown, and you don't want to log each of them because they are the same.
Use LOG_SLACK_CACHE_SECONDS
(uses Laravel cache under the hood) to suppress output for X seconds, or pass it to the cacheSeconds
function.
use Stasadev\SlackNotifier\Facades\SlackNotifier;
SlackNotifier::cacheSeconds(60)->send(new \RuntimeException('Test exception'));
composer test
Inspired by spatie/laravel-slack-alerts.
The MIT License (MIT). Please see License File for more information.