Slack for monitoring your Laravel-App

Rohit Shirke
4 min readMar 24, 2019
Image result for laravel + slack image
Slack notification with laravel

Hi all,

Have you ever used slack for chat, sharing files or to collaborate with your teams on work? if no then (@slack is an awesome application to collaborate with teams and it comes with tones of great features like multiple type file sharing,bots and many more…) else continue here 😛…well, I was just wondering if, I can use slack to monitor my web applications and came up with this simple solution.

(A Friendly Note : Yea, I know that, #laravel already comes packed with support for slack notifications but, at the time of writing this, i had not explored that option throughly and, i wanted something more custom and along-side i was willing to explore more about slack apps😀. So, if you want your are still free to use build-in notification support.)

So, lets begin,

Here, we are going to create a slack application, which will provide us a slack web-hook endpoint, that allows pushing updates/messages from external source to slack chat room.

Configure Slack .

  1. Open slack and create a slack Team. I will call it “error_logs”. (just to keep your workspace clean. )

2. Now, Head to https://api.slack.com/apps/new & create a slack App.

create slack App.

Give your App a name (in my case its “Error Reporter”) and select a workspace where you want to add this slack App.

In the next window, you will get many options that allow you to extend many other features of slack but, for now we are only focusing on pushing data to slack room so check out for “Incoming Web-hooks” section.

Slack UI to configure incoming webhooks

Click on incoming web-hooks box.

On next page you will be shown a page where you will have to simply toggle “Activate incoming webhooks” button.

Activate incoming webhooks.

Now, Scroll down to the page and click on “Add new webhook to workspace”.

Add new slack webhook

On the next page, slack will ask you to authorize newly created App to, allow it for posting messages on your slack chat room. (In my case its “error_logs”)

Authorize Slack app to your slack room.

on successful authorization, we will end with something similar to one below.

Your slack end-point to push updates to slack chat room.

We are done at the slack end, now lets handle error reporting in our laravel App!

Configure laravel App :

I am using laravel’s build in error-handling fall back mechanism here. (If you want to read more, head over to https://laravel.com/docs/5.7/errors)

Open your laravel application in some editor and head over to the file App\Exceptions\Handler.php. This file contains only two methods report() and render() that allows us to handle & report exceptions gracefully.

As, we only want to report errors for now so, we will leave error handing onto default mechanism and only focus on the report method. following is the piece of code that does it.

If you are curious to know what above piece of code is doing then, its just create a simple post request to slack webhook-endpoint.

(Note : I have used GuzzelHttp\Client for creating a post request to web-hook endpoint you are free to use curl or any other equivalent method. If you have copied the above code then don’t forget to composer require guzzelhttp/guzzle )

And,

We are done!

Just head to your application and create a dummy exception and if everything is configured properly you will receive a message in your slack chat room.

Slack notification for laravel app.

Hope, you enjoyed!

Happy monitoring! :D

--

--

Rohit Shirke

Tech enthusiastic | Tech Lover | Software Developer