Super simple way to write log in Laravel

The easiest method ever to write log to file

3 years ago SETHA THAY 2139
Super simple way to write log in Laravel

Yes, super simple !!! If you find other articles difficult to understand about writing log in laravel, here this post is for you. You can enable and help to understand what is happening inside your system, program, or function by following a few steps of logging. Logging is a common way that helps programmers debug errors or understand processes and logic by log messages to files or to broadcast to channels such as Slack.

Laravel logging is based on "channels". Each channel represents a specific method of writing log information. For example, the "single" channel writes log files to a single log file (Usually laravel.log file), while the "slack" channel sends log messages to Slack. Log messages may be written to multiple channels based on their severity or log level (We will explain severity later below).

STEP 1: Modify config/logging.php

Configuration options of logging behavior are placed in the config/logging.php. This file allows us to configure log channels. By default, laravel will use a stack channel when logging messages. The stack channel is used to aggregate multiple log channels into a single channel. Follow the below instruction to add you own channel

  1. Define your own channel
    Define "dumplog" as channel name with driver "single" and log level "info". The log information will be store in file dumplog.log
  2. Adding your channel into the stack channel
    Adding "dumplog" into stack channel as stack channel is the default one.

super-simple-way-to-write-log-in-laravel-define-channel

STEP 2: Let's Start to Log Your Information

You can now use facade "Log" to write information about function process flow, logic description into the log file.

use Illuminate\Support\Facades\Log;

Before continue, let me just talk about log levels. Log level is an option that determines the minimum "level" a message must be in order to be logged by the channel. Monolog, which powers Laravel's logging services, offers all of the log levels defined in the RFC 5424 specificationemergencyalertcriticalerrorwarningnoticeinfo, and debug.

You can use the log level as listed above. So, imagine we log a message using the debug method:

Log::debug('An informational message.');

If we use log level as info, we have to use the below syntax to log

Log::info('The system is down!');

In this example, I use log level info to generate messages before running the script and log again when the process is finished or the process is failed.

super-simple-way-to-write-log-in-laravel-define-log-code

Here is the result that the information has been logged in file dumplog.log

super-simple-way-to-write-log-in-laravel-result

THANK YOU !!!

Find Us @
Facebook
Telegram
Twitter
LinkedIn


About author

Author Profile

SETHA THAY

Software Engineer & Project Manager. I am willing to share IT knowledge, technical experiences and investment to financial freedom. Feel free to ask and contact me.



Scroll to Top