Skip to content

stasadev/laravel-slack-notifier

Repository files navigation

Stand With Ukraine

Laravel Slack Notifier

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

Send exceptions and dump variables to Slack.

use Stasadev\SlackNotifier\Facades\SlackNotifier;

SlackNotifier::send(new \RuntimeException('Test exception'));
SlackNotifier::send('Test message');

Installation

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"

Usage

To send a message to Slack, simply call SlackNotifier::send().

Report Exception

// 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);
}

Dump Variable

use Stasadev\SlackNotifier\Facades\SlackNotifier;

$variable = 'message';
// $variable = ['test' => 'array'];
// $variable = new stdClass();

SlackNotifier::send($variable);

Using multiple webhooks

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');

Using a custom webhooks

The to function also supports custom webhook URLs.

use Stasadev\SlackNotifier\Facades\SlackNotifier;

SlackNotifier::to('https://custom-url.com')->send('Test message');

Sending message to another channel

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');

Slack bot customizing

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');

Formatting

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,

Additional context in the message

Include additional context in a Slack message (use dont_flash to exclude sensitive info from context). It will be added as an attachment.

Exception stack trace filtering

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.

Caching the same exceptions

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'));

Testing

composer test

Credits

Inspired by spatie/laravel-slack-alerts.

License

The MIT License (MIT). Please see License File for more information.