At some point debugging options get limited, and error reporting starts to become a headache, it was at this point that I found Laravel logging to slack channels to be very helpful, however logging to slack channels can be used for more various purposes.

The problem

I was developing an android application using Vue Native (a framework that adds Vuejs syntax and features on top of React Native framework) and back end by using Laravel. I was testing Laravel REST API using Postman.

There was one end point which when I tested with Postman it worked very well, but when I tested with the app the response was null. From there I started to find ways to at least see the output from the back end. The problem was that the react native debug console couldn’t show output because either you display the response object that is not parsed and you can’t see out put from the server or you parse the response to JSON which it fails and brings an error, using Laravel’s dd(), dump(), or echo also did not help because the response could not be parsed to JSON.

The idea

My first solution was to use Laravel’s Logging. Which the first idea was to use the default logging channel ‘stack’ which used the driver ‘single’. This logging driver logs all the messages to the storage/logs/laravel.log file. This meant every time I sent the request, then a message had been logged to file, I had to open the file, scroll to bottom, then check the message. In case another request was sent, I would have to close the log file and open it again to see the changes. This is very unproductive, inconvenient and ineffective.

To help you learn more about what’s happening within your application, Laravel provides robust logging services that allow you to log messages to files, the system error log, and even to Slack to notify your entire team.

Slack solution

Using slack you can log messages from Laravel app straight to slack channel, and using slack desktop or slack mobile app, you get straight almost instantaneous push notification with the log message. This was the best solution for me, which meant that I didn’t have to open a file every few minutes just to check the log updates. This was far more convenient and efficient. From there I could easily log the messages and see what was happening in the server!

Implementation

Create a channel in slack

Go to slack, click get started and then create a slack workspace. It should request for your email. Enter your email then it will send you a verification code. Then fill a few concise details then you will finally have your channel. I called mine laravelslack.

Add Slack Web-hook

Click see your channel in Slack, then on the left side menu, click on Apps, it should open a search bar with multitude of slack apps. In the search bar write “webhook” and click add on the first result , “Incoming WebHooks”. Again click “add to slack”, and it should bring you a form in which you can select the channel we created in the previous step. Finally click “Add Incoming Webhooks Integration”. This will generate a webhook URL which you can copy and use it in the Laravel application.

Laravel Integration

In your Laravel application, add the value “LOG_SLACK_WEBHOOK_URL=” to your .env file, then assign the webhook URL which you obtained from the previous step. This will make your Laravel application send log messages to the Log channel you just created.

Inside config/logging.php, in the channels array, modify the key ‘channels’ inside the ‘stack’ driver and add value ‘slack’, this will tell Laravel to log to both the single channel and slack channel

You may have to clear the config cache and cache the new configurations with the artisan command,

php artisan config:cache

Finally, usage

Any where in your Laravel application you may use the ‘Log’ facade to write your log messages. Laravel supports logs to slack channel only from level critical, this means that debug logs wont be sent to the slack channel.

In slack channel,

Other applications

  1. Log general error messages inside try catch block to slack for developer teams to see the errors and to get notified every time they happen to fast support and efficient error reports.
  2. Script notifications, example writing a script that continuously checks for new information from an API, then notifies you in case new entries are found, or any notifications that may be required from an automated script. Alternative option may be email.
  3. Laravel notifications, for various user events.

Conclusion

Slack is a tool that has a wide range of applications. Combined with Laravel it provides simple missing reporting bridge that developers would benefit the most from. Not all Laravel applications require slack integration, this very much dependent on the use case, if misused it will add an unnecessary complexity to your Laravel app.

“Quality must be enforced, otherwise it won’t happen. We programmers must be required to write tests, otherwise we won’t do it.”
Yegor Bugayenko, Code Ahead: Volume 1

Laravel 7 + Dependency Injection: Cart implementation in an eCommerce website, Service Container…
How I used service container to create a flexible cart that uses database or session implementation depending on user…medium.com

Laravel 7 + Redis: Supercharge your Laravel application
Redis is one of those diamonds in the dirt most new Laravel Developers ignore when starting upmedium.com