) Log4php usage and Configuration Guide

Source: Internet
Author: User
Http://www.qinbin.me/log4php-use-and-configuration-guide/

It has always been a temporary log component, but with the continuous improvement of functions, program logs are also standardized. However, I feel that the current log function cannot meet the current requirements, therefore, considering introducing a complete log component, it is nice to look at log4php.

Log4php is one of the log4xx series log components. It is a version from log4j to PhP. It is mainly used to record log information and supports multiple input destinations, including: log Files, log rollback files, databases, and log servers. Multiple Input formats are also supported.

Instance used:

1234567 require_once 'log4php/logger. PHP '; logger: Configure ('logger. properties '); $ logger = logger: getrootlogger (); // $ logger-> debug ("Hello world! "); // $ Logger-> Info (" Hello world! "); // $ Logger-> warn (" Hello world! "); // $ Logger-> error (" Hello world! ");

Appender. default = loggerappenderrollingfile log4php. appender. default. file = logger. log. PHP log4php. appender. default. layout = loggerlayoutpattern log4php. appender. default. maxfilesize = 1024 log4php. appender. default. maxbackupindex = 10 log4php. appender. default. layout. conversionpattern = "% d {iso8601} [% P] % C: % m (at % F Line % L) % N" log4php. rootlogger = debug, default

Configuration item description:

Log4php consists of three important components:

Log Information priority;

The log output destination;

The output format of log information.

The log priority ranges from high to low, including error, warn, info, and debug, which are used to specify the importance of the log information; the log output destination specifies whether the log will be appended to the end of the execution file or the file. The output format controls the display content of the log information.

1. Log priority.

There are four types of log information (commonly used). You can set a priority in the configuration file to display or hide debugging information in the Code. For example, when I adjust the log level to the warn level, the error information in the Program (through $ log-> error ("error message here! ")) And warn level information can be recorded, and all information using info and debug level in the program will not be displayed.

"Log4php. the first parameter definition of rootlogger in rootlogger = debug, A1 "means to define the debugging level as debug level, that is, all the errors, warn, info, debug information can be displayed. If the level is set to info, only three types of information, namely error, warn, and info, can be displayed.

The following are four common usage levels:

Debug level indicates that fine-grained information events are very helpful for debugging applications.

Info level indicates that messages emphasize the running process of applications at the coarse granularity level.

Warn level indicates a potential error.

Error level indicates that although an error event occurs, the operation of the system is not affected.

Fatal level indicates that each serious error event will cause the application to exit.

2. log output destination. ,

Log4php supports 12 output destinations:

<? PHP loggerappenderconsole // Use PHP: // stdout as the output location loggerappenderdailyfile // inherits from loggerappenderfile and uses the file as the output location, output a file loggerappenderdb every day // loggerappenderecho with the database as the output location // output loggerappenderfile at the end of the execution file // loggerappendermail with the file as the output location // loggerappendermailevent with the mail as the output location from loggerappendermail, loggerappendernull triggered by an email as the output location and no information is output. loggerappenderphp // The error information is output to PhP, and various log levels are converted to the standard PHP information loggerappenderrollingfile // step From loggerappenderfile, with xxx. log.1, xxx. loggerappendersocket // output loggerappendersyslog in the form of socket // output loggerappendersyslog in the form of system logs as the destination, using the syslog () function in PHP for record?>

In the configuration file, "log4php. rootlogger = debug, default" is the alias starting from the second parameter. The specific destination setting method is as follows:

12 log4php. appender. Default = loggerappenderecho log4php. appender. Default. layout = loggerlayouthtml

Each output destination has different set parameters. according to actual needs, there are multiple output destinations, as shown below:

Log4php. rootlogger = debug, default, qinbin1, qinbin2 ,....

Default, qinbin1, and qinbin2 indicate different output destinations.

3. log file output format. As an attribute of the output destination, log4php contains five different output methods:

Loggerlayouthtml // output debugging information in HTML Format

Loggerlayoutsimple // simple display in "level information-log information" Format

Loggerlayoutttcc // displayed in the format of "Monthly, daily, and yearly [process] level information log name-debugging information"

Loggerpatternlayout // use a pattern expression as the output format. (This function is extremely powerful. You can set the output format as you like. For more information, see the instance section)

Loggerxmllayout // output in XML format

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.