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 .
- 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.
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.
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.
Now, Scroll down to the page and click on “Add new webhook to workspace”.
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”)
on successful authorization, we will end with something similar to one below.
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
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 )
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.
Hope, you enjoyed!
Happy monitoring! :D