Send a slack notification every time Log::error is triggered

Edit: Broken as of Laravel 5.6

The Log facade is really just a wrapper for an underlying instance of Monolog. The good news is Monolog comes with support for Slack. You just need to tell Monolog to use it.

With that said, everything can be setup in 3 lines of code.

$monolog = \Log::getMonolog();
$slackHandler = new \Monolog\Handler\SlackHandler('your-token', '#your-channel', 'Monolog', true, null, \Monolog\Logger::ERROR);
$monolog->pushHandler($slackHandler);

Then to get this running, you can either create your own service provider for it or just drop it in AppServiceProvider's boot method.

You may want to look at the source code for SlackHandler just in case there are more options the constructor takes which you would need to use.

Now whenever you \Log::error('some error');, that error's message will be sent to the Slack channel you have setup. Please note this "bubbles up" which means it will be sent to the Slack channel for any logging done error and up, error, critical, alert, and emergency. Set the bubble parameter to false if you only want it to log errors.


For Laravel 5.6 and above:

Laravel suppports slack driver for logging beginning with 5.6. Configure your slack channel in config/logging.php like so:

'slack' => [
    'driver' => 'slack',
    'url' => env('LOG_SLACK_WEBHOOK_URL'),
    'username' => 'Laravel Log',
    'emoji' => ':boom:',
    'level' => 'critical',
],

Now all the log messages which have an importance critical or above will automatically logged to slack channel.

You can also log to slack channel specifically:

Log::channel('slack')->info('Something happened!');

More info in Laravel logging and Slack incoming webhooks.


You can listen to illuminate.log [String $level, String $message, Array $context] event. If $level equals error, you will send notification.

Define event listener in your EventServiceProvider

protected $listen = [
    'illuminate.log' => [
        'App\Listeners\LogEventListener'
    ]
];

This will tell Laravel to fire LogEventListener when illuminate.log event is fired.

Then create this listener:

namespace App\Listeners;

use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;

class LogEventListener
{
    /**
     * Create the event listener.
     *
     * @return void
     */
    public function __construct()
    {
        //
    }

    /**
     * Handle the event.
     *
     * @param  DeployDisabledEvent  $event
     * @return void
     */
    public function handle($event)
    {
        if ($event->type == 'error') {
            $this->notifyViaSlack($event->message, $event->context);
        }
    }

    /**
     * Send Slack notification.
     *
     * @param  string  $message
     * @param  string  $context
     * @return void
     */
    protected function notifyViaSlack($message, $context)
    {
        /*
         * Slack notification logic
         */
    }
}