Laravel Integrated Monolog Library to configure and record instances of logs

Source: Internet
Author: User
Tags php file syslog

For large systems, the log is an indispensable module, Laravel Nature also provides a perfect support for the log. At present, Laravel integrates a powerful Monolog library for logging. Let's take a rough look at how to configure the log and the simple use of the log in Laravel.

1. Log Processor Configuration

First, let's look at the configuration of the log processor. Laravel currently supports four kinds of log processors:

single--logs to a single file. The log processor corresponds to the Monolog streamhandler.
daily--Archives The log as a date, creating a new log file record log every day. The log processor corresponds to the Monolog rotatingfilehandler.
syslog--logs to the syslog. The log processor corresponds to the Monolog sysloghandler.
errorlog--logs to the Error_log in PHP. The log processor corresponds to the Monolog errorloghandler.

The project's actual log processor is determined by the log configuration entry in config/app.php, and the default configuration value is single. Here we use the default values and do not make changes:

' Log ' => ' single ',

Of course, if these four ways do not meet your needs, you can also use the Configuremonologusing method to fully control the Monolog log processor:

$app->configuremonologusing (function ($monolog) {
$monolog->pushhandler (...);
});

Note: The above code must be placed before the bootstrap/app.php file returns to $app before it can take effect.
2. Log Logging using log
Once the configuration is complete, log facades can be used in the code to record logs, behind which the log façade is actually illuminate\log\writer, and Monolog\logger is injected into the Writer constructor. The generated log files are stored in the Storage/logs directory.

Currently, log façade supports eight logging levels (using RFC 5424 standard):

Log::emergency ($error); Emergency, like the system is dead.
Log::alert ($error); Need to take immediate action issues, such as the whole station down, database anomalies, etc., this situation should be reminded by SMS
Log::critical ($error); Serious problems, such as invalid application component, unexpected exception
Log::error ($error); Run-time errors that do not require immediate processing but need to be logged and monitored
Log::warning ($error); Warning but not an error, such as using an obsolete API
Log::notice ($error); An ordinary but noteworthy event
Log::info ($error); Events of interest, such as login, exit
Log::d ebug ($error); Detailed debugging information

Let's take a separate demo of these log-level logs, which we'll test in the TestController log method:

Public Function log () {
Log::emergency ("The system hangs off");
Log::alert ("Database access exception");
Log::critical ("System unknown Error");
Log::error ("The specified variable does not exist");
Log::warning ("The method has been discarded");
Log::notice ("User log in Offsite");
Log::info ("User xxx login succeeded");
Log::d ebug ("Debug Information");
}
To access Http://laravel.app:8000/test/log in the browser, log information corresponding to the Storage/logs/laravel.log record is as follows:

[2015-11-09 14:24:05] Local. Emergency: The system is dead.
[2015-11-09 14:24:05] Local. ALERT: Database Access exception
[2015-11-09 14:24:05] Local. CRITICAL: An unknown error occurred in the system
[2015-11-09 14:24:05] Local. ERROR: Specified variable does not exist
[2015-11-09 14:24:05] Local. WARNING: This method has been discarded
[2015-11-09 14:24:05] Local. NOTICE: Users log in Offsite
[2015-11-09 14:24:05] local.info: User XXX Login Succeeded
[2015-11-09 14:24:05] Local. Debug: Debugging Information

It is visible that the corresponding log records contain information such as logging time, log level, and log messages. Of course, we can also pass context information when logging:

Log::info ("User xxx login succeeded", [' user_id ' =>1]);
The corresponding log records are:

[2015-11-09 14:25:47] local.info: User xxx Login succeeded {"user_id": 1}

If you want to access an instance of the underlying monolog, you can use the following methods:

$monolog = Log::getmonolog ();
DD ($monolog);

We can view the property information for the current Monolog object instance in the browser page:

We can see the current use of the log processor, the location of the log record, the format of the logging and other information.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.