Laravel: Sending error message via Slack

I wanted a was to send error message from my Laravel application to a slack channel so I would know of issues before a user has to contact me. There were a lot of examples using Monolog SlackHandler, but you have to make an app and generate a token for it. From a note on this Stack Overflow thread I saw you can use webhooks now.

It was quite simple. Here’s how I set is up.

In the /app/Providers/AppServiceProvider.php file in the boot function I added:

// Send errors to slack channel
$monolog = Log::getMonolog();
if (!\App::environment('local')) {
 $slackHandler = new SlackWebhookHandler(env('SLACK_WEBHOOK'), '#development', 'App Alerts', false, 'warning', true, true, Logger::ERROR);
} else {
 $slackHandler = new SlackWebhookHandler(env('SLACK_WEBHOOK'), '@wes', 'App Alerts', false, 'warning', true, true, Logger::ERROR);
 }
 $monolog->pushHandler($slackHandler);

So now when I am developing the errors are sent directly to me, on production they are sent to the development channel.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s