Laravel 8/9 Logs Management with Example

This tutorial helps to understand laravel 8/9 logging. The logging help to understand what’s happening in your application. You can also identify the error line with the correct understandable exception message.

We will create different types of logs message into the log file. You can view the logs message in log file, see into the shell command window.

Laravel Log

The default channel is a log file and stored into the /storage/logs/laravel.log file. 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.

Laravel logs is depend on the channel, You can define channel information in the config/logging.php configuration file. The Log messages may be written to multiple channels based on their severity. By default, Laravel will use the stack channel when logging messages.

Each channel writes log information in a specific way, Like the single channel writes log files to a single log file, while the slack channel sends log messages to Slack.

How To Configuring The Channel Name

The laravel log internally utilizes the Monolog library, Which provides support for a variety of powerful log handlers.

There is a following sample channel configuration :

'stack' => [
'driver' => 'stack',
'name' => 'channel-name',
'channels' => ['single', 'slack'],
]

Where params are :

  • driver: The driver determines how and where the log message is actually recorded.
  • name: This is your channel name.
  • channels: array of channels.

There are the following channels available in Laravel :

NameDescription
customA driver that calls a specified factory to create a channel
dailyA RotatingFileHandler based Monolog driver which rotates daily
errorlogA ErrorLogHandler based Monolog driver
monologA Monolog factory driver that may use any supported Monolog handler
nullA driver that discards all log messages
papertrailA SyslogUdpHandler based Monolog driver
singleA single file or path based logger channel (StreamHandler)
slackA SlackWebhookHandler based Monolog driver
stackA wrapper to facilitate creating “multi-channel” channels
syslogA SyslogHandler based Monolog driver

How to Define Multiple Log Channels in Laravel

You can define multi-channel in laravel logs as below:

'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['syslog', 'slack'],
],
'syslog' => [ 'driver' => 'syslog', 'level' => 'debug', ], 
'slack' => [ 
'driver' => 'slack', 
'url' => env('LOG_SLACK_WEBHOOK_URL'), 
'username' => 'Laravel Log', 
'emoji' => ':boom:', 
'level' => 'critical', ],
],

The level parameter holds the severity of the message, if the message is debugging then it will be logged by syslog channel. if the log message type is critical, then it will be logged by the slack channel.

we log a message using the debug method:

Log::debug('Debug message: Hi, I am phpflow.');

syslog channel will write the above message to the system log.

we log a message using the critical method:

Log::critical('critical message: Hi, I am phpflow.);

Any other type of log message will be written into both types of channels. There are the following other types log messages available in the Laravel:

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

How To Log Contextual Information

An array of data may also be passed to the log methods. This array of data will be formatted and displayed with the log message:

Log::info('The service response is', ['resp' => $resp]);

Laravel Log message to Specific Channel

Sometimes you may wish to log a message to a specific channel other than your application’s default channel. You need to use channel() method to log message:

use Illuminate\Support\Facades\Log;
Log::channel('slack')->info('Something happened!');

Conclusion:

We have learned the log management system in laravel 8, defined the channel configuration, and use with different types of log messages. We can send the log message into the specific channel.

Leave a Reply

Your email address will not be published. Required fields are marked *