Send Laravel Log to Slack Notification
Laravel provides robust logging services that allow you to log messages to Slack to notify your entire team.
There are two main and very easy steps to prepare Slack and Laravel to connect with each other.
Before you continue with the rest of this tutorial, I would like to introduce two packages for Laravel that I have recently developed: Laravel Pay Pocket, a modern multi-wallet package, and Laravel Failed Jobs, a UI for the Laravel Failed Jobs Table. I hope they may be of help to you.
https://github.com/HPWebdeveloper/laravel-pay-pocket
https://github.com/HPWebdeveloper/laravel-failed-jobs
Step 1- Slack Part:
In slack dashboard go to: Administration / Manage apps
Then in the Slack search box type: Incoming WebHooks
Then you will see the Slack Incoming WebHooks app information page:
Press the red button of Add to Slack.
In the new window it will ask you to select one of your slack channels and then press Add Incoming WebHooks Integration.
Then a new window will show you the WebHooks URL:
At the end of this page you see the button of Save Settings. Press it.
As soon as the WebHooks url is generated you will see a notice inside the channel you selected like below:
Copy the WebHooks URL We need to set it in Laravel .env file.
Step 2- Laravel Part:
Now put this line in Laravel .env file
LOG_SLACK_WEBHOOK_URL= WebHooks URL we obtained in Step 1
In Laravel 6 the config file of Logging is different with older versions. Let’s have a look at config/logging.php file.
By default, Laravel will use the stack
channel when logging messages. The stack
channel is used to aggregate multiple log channels into a single channel. Which after a fresh Laravel installation it is set to only accept ‘daily’.
So edit the config/logging.php file and set ‘slack’ as the second channel for logging Laravel messages as below.
Then since we changed the .env and a config file we need to clear and again cache Laravel configurations by:
php artisan config:cache
As you see in slack channel the level of Logging is set to critical!
The level option determines the minimum “level” a message must be in order to be logged by the channel. All of the log levels defined in the RFC 5424 specification: emergency, alert, critical, error, warning, notice, info, and debug. You may write information to the logs using the Log
facade like:
Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);
So it means If we want to send a message to a Slack Channel using default settings we need to:
Log::critical($message);
Then in the web.php file I do this change:
Route::get('/', function () {
Log::critical('This is a critical message Sent from Laravel App');
return view('welcome');
});
and refresh (trigger) the Laravel app main page.
And you will see in your slack channel the message:
An unrelated tip:
Shorthand of:
Log::info($message);
is
info($message);
Note: one interesting use-case for notifying the event to Slack is putting log::info in the Laravel Model Observer:
<?php namespace App\Observers;
use Log;class UserObserver {public function created($user)
{
Log::info('User created!');
} public function saved($user)
{
Log::info('User saved!');
}
}
Which depending to the sensitivity, it can be Log::critical or Log::info.