This tutorial help to understand laravel 8 logging. The logging help to understand what’s happening into your application. You can also identify the error line with correct understandable exception message.
We will create different types of logs message into the log file. You can view the logs message into log file, see into the shell command window.
The default channel is log file and stored into the
/storage/logs/laravel.log file. The 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 channel, You can defined channel information into 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 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, That provides support for a variety of powerful log handlers.
There is a following sample channel configuration :
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 following channels available in Laravel :
How to Define Multiple Log Channels in Laravel
You can define multi-channel in laravel logs as below:
The level parameter hold the severity of the message , if the message is debug then it will be logged by syslog channel. if the log message type is critical, then it will logged by 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 log message will be write into the both type channels. There are following other types log message available into the Laravel:
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:
We have learn log management system in laravel 8, defined the channel configuration and use with different types of log message. We can send the logs message into the specific channel.
Originally published at https://www.phpflow.com on May 4, 2021.