Laravel 8 Logs Management with Example — Phpflow.com

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.

Laravel Log

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

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

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

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

Laravel Log message to Specific Channel

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

Conclusion:

Originally published at https://www.phpflow.com on May 4, 2021.

Hey, I am Parvez Alam. A software developer since 2009. I am a programming addict. I love learning and sharing knowledge. https://www.phpflow.com/