Use of log4php
First introduce the logger.php file. Log4php can complete the automatic loading process by introducing logger.php. The file location is as follows:
The logger itself does not define the output destination and format of the log, so we usually need to introduce a configuration file after introducing log4php (described in detail below). A logger is a component that we use to log information through a logger. After the configuration file is introduced, a logger entity is obtained via GetLogger. You can then print out the log information by printing the method. Printing methods typically include info, warn, error, debug, and so on.
Include (' logger.php '); Logger::configure (Common_path. ' conf/log.php ');//introduction of configuration File $logger = Logger::getlogger ("main"); Generate the Log instance $logger->info ("This was an informational message."); Display informational messages in a well-defined output destination in a regulated output format
log4php Configuration
The log4php configuration file supports formats such as XML, PHP, Properties (INI), and so on. Also supports programmable configuration, which is the function to change the configuration.
Class Myconfigurator implements Loggerconfigurator {Public function Configure (Loggerhierarchy $hierarchy, $input = NULL) {//Create an appender which logs to file $appFile = new Loggerappenderfile (' foo '); $appFile->setfile (' d:/temp/log.txt '); $appFile->setappend (TRUE); $appFile->setthreshold (' all '); $appFile->activateoptions (); Use a different layout for the next appender $layout = new Loggerlayoutpattern (); $layout->setconversionpattern ("%date%logger%msg%newline"); $layout->activateoptions (); Create an appender which echoes log events, using a custom layout//And with the threshold set to INFO $appEcho = new Loggerappenderecho (' Bar '); $appEcho->setlayout ($layout); $appEcho->setthreshold (' info '); $appEcho->activateoptions (); Add both appenders to the root logger $root = $hierarchy->getrootloggeR (); $root->addappender ($appFile); $root->addappender ($appEcho); }}
Logger::configure (Common_path. ' conf/log.php ');
The project is introduced into the configuration by this line of code. The configuration typically includes the following:
1. Priority of log information; 2. The output destination of the log information; 3. The output format of the log information.
A log4php Log Information Priority
The priority level of log information is from high to low with error, WARN, info, DEBUG.
Here are the usage of four different levels:
The debug level indicates that fine-grained information events are very helpful for debugging applications.
The INFO level indicates that the message highlights the application's running process at a coarse-grained scale.
WARN level indicates a potential error situation.
The error level indicates that the system continues to operate without affecting the failure event.
The program can only display information that is lower than the level priority level that is currently set. For example, the current program setting level is debug, which means that all the information in the program can be displayed. If the current program level is info, then only info,warn,error three of the log information can be displayed.
Return Array (' rootlogger ' = = Array (' appenders ' = = Array ( ' Myconsoleappender ', ), ' Level ' = ' DEBUG ' ),
Two Output address configuration
As with priority, the output address can also be configured in the configuration file, and different output destinations can be set according to different log categories. Cases:
' Loginfileappender ' = [' class ' = ' Loggerappenderdailyfile ', ' layout ' and ' = ' class ' = ' Loggerlayoutpattern ', ' params ' = = [ ' conversionpattern ' = '%date ' [%logger]%message%newline ', ], ], ' params ' = [ ' file ' = ', '/log/login/login_%s.log ', ' datepattern ' = ' y_m_d ', ] ],
The log4php supports 12 types of output destinations, namely:
Loggerappenderconsole with Php://stdout as the output destination
Loggerappenderfile file as the output destination
Loggerappenderdailyfile file as the destination, output a file daily
Loggerappenderdb the database as the output destination
Loggerappenderecho performing file tail output
Loggerappendermail the mail as the output place
Loggerappendermailevent the message as the output, triggering the event
Loggerappendernull does not output any information
loggerappenderphp loggerappenderphp output to PHP error message to convert various log level information to PHP standard Information
Loggerappenderrollingfile output in the form of Xxx.log.1, xxx.log.2
Loggerappendersocket Output as Socket
Loggerappendersyslog the System log as the destination output, using the Syslog () function in PHP to record
The code specifies that a file is exported daily as the output destination, which is used to record log information for the logon type.
Three Log file output format
It exists as an attribute of the output destination, and the log4php contains 5 different output modes, respectively:
loggerlayouthtml output debug information in HTML format
Loggerlayoutsimple simple display in "Level information-Log message" format
LOGGERLAYOUTTTCC is displayed in the format "month/day/year time [process] level information Log name-debug Information"
Loggerlayoutpattern is displayed as an output format as a pattern expression (this mode can be customized in the output format)
loggerxmllayout//output in XML mode
Some format parameters of the Loggerlayoutpattern mode
Loggerlayoutpattern provides us with a way to customize the format of log information, which is often used in projects to customize log information. In practical use, we define the pattern by configuring the Conversionpattern parameter. The parameter variables are typically configured with some conversion specifiers provided by the log4php.
For example:
' Conversionpattern ' = '%date [%logger]%message%newline '
The format is: yyyy-mm-ddthh:mm:ss+08:00. [Log type]. The contents of the log. Line break.
The commonly used conversion specifiers are:
-
%logger (%lo,%c) The name of the logger for the request log.
-
%data ($d) time. The default is ISO8601 format. You can change the format by%data{(format)}.
-
%location (%l) The location of the caller
-
%message (%m%msg) The contents of the log
-
%n (%newline) line break
-
%level (%p) Priority of current log events