Logging messages to a slack channel or multiple slack channels with Laravel

Nazem Mahmud Piash
4 min readJan 5, 2023

Laravel provides a logging service that allows logging messages to Slack. It helps to notify the entire team quickly. The steps are very easy to connect Laravel with Slack and send the log message. This is tested with version 6, 7 & 8.

The following 2 steps are for sending log messages to a single Slack channel.

Step 1: Create a webhook URL in Slack

  • From your workspace in Slack app, in the left-top corner, go to Administration / Manage apps
  • In the new version of slack, you can find it under Tools & Settings:
  • Then in the browser, in Slack search box, type Incoming WebHooks
  • This will redirect you to the Slack Incoming WebHooks app page
  • Press the Add to Slack button.
  • In the new window, select one of your slack channels. Then press Add Incoming WebHooks Integration.

A new window will show you the WebHooks URL and other necessary info. At the end of that page, you will find a Save Settings button. Save the information.

You will see a notice in your slack channel as soon as the WebHooks URL is generated

  • Copy that WebHooks URL. You have to add that URL in your Laravel .env file.

Step 2: Integrate the webhook URL with Laravel

  • Put the WebHook URL that you copied in the .envfile like below
LOG_SLACK_WEBHOOK_URL= WebHooks URL we obtained in Step 1

You can choose other variable name. It is user defined.

  • Now go to the config/logging.php file. Any version below Laravel 6, may have different logging file. You will see, by default, Laravel will use the stack channel for logging messages. This channel aggregates multiple log channels into a single channel.

You can change the log channel name in the .env file by setting the LOG_CHANNEL= value, from one of the channelsname in logging file list. This variable name in the .env file is provided by Laravel. You can’t change it.

  • Edit the config/logging.php file and set ‘slack’ as the second channel for logging Laravel messages beside single like below. And, also you can update your slack channel as you see fit like below
'default' => env('LOG_CHANNEL', 'stack'),

'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'slack',],
'ignore_exceptions' => false,
],

'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => env('LOG_LEVEL', 'debug'),
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => env('LOG_LEVEL', 'critical'),
],

The leveloption indicates 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: You may write information to the logs using the Log facade like following:

Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);

You can choose any of the above log levels as you see fit other than critical.

Now you can test from any API. Say, create a route in your web.php file in routes folder like following:

Route::get('/slack-test', function () {
Log::critical('This is a critical message Sent from Laravel App');
return view('welcome');
});
  • As we changed our .envand a config file, let’s clear the cache using the following command:
$ php artisan optimize

If you go to the page now, you will see in your slack channel a message like this:

You can use this Log facade in your try…catch block, like the following:

use Illuminate\Support\Facades\Log;
...

public function slackTest(Request $request)
{
try {
// your code...
} catch (Exception $e) {
Log::critical('your critical message');
throw new Exception($e->getMessage(), $e->getCode());
}
}

Log to multiple channels

Let’s say you want to keep different types of logs in different slack channels. To do this,

  • follow “Step 1”, and generate another WebHooks URL for another slack channel.
  • Set that URL value in you .env file with another variable name, like this
LOG_ANOTHER_SLACK_WEBHOOK_URL=your=webhook-url
  • Create a new custom channel in your config/logging.php file in the channels list:
'channels' => [
// ...

// slackNotificationLog is a custom name
'slackNotificationLog' => [
'driver' => 'slack',
'url' => env('LOG_ANOTHER_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Another Log',
'emoji' => ':boom:',
// some other configuration if you need as a key-pair value
],
  • You can use it like the following:
<?php

Log::channel('slackNotificationLog')->info('New log is created');

You can find more about the Log facade in the official documentation for log format.

--

--

Nazem Mahmud Piash

Senior Software Engineer || Node, ExpressJs, Laravel, React, Vue, Angular