Detailed configuration example of log4ph3169

Source: Internet
Author: User
Detailed configuration example of log4ph3169 1. what is log4php: log4j is a well-known log development kit in JAVA, which organizes maintenance projects for apche, vxR brother uses php to implement the log4j function. Currently, log4php has been used as a detailed configuration example for log4ph9 9 of log4j.

1. what is log4php:
Log4j is a well-known log development kit in JAVA. it organizes maintenance projects for apche. VxR brother uses php to implement the log4j function. Currently, log4php already exists as a sub-project of log4j, for details, click "renew.
: Http://www.vxr.it/log4php/download.html

II. installation:
This document downloads log4php-0.9.tar.gz. after decompression, the directory contains the src Directory. copy the {decompressed directory}/src/log4php/directory to your project directory to complete installation. Because this article is used to explain log4php, the irrelevant directories are not listed and defined as http: // localhost. the Project structure is as follows:
+ App/
+ Log4php
+ Images/
+ Logs/
+ Js/
+ Css/
-Log4php. properties
-Index. php

III. Getting started:
First, let's take a look at the content in test1.php:
[Php]
/* 1 */define (LOG4PHP_DIR, "log4php ");

/* 2 */require_once (LOG4PHP_DIR. '/LoggerManager. php ');

/* 3 */$ str = "here is test string! ";

/* 4 */echo "the output of PHP is irrelevant to log4php!
";

/* 5 */$ logger = LoggerManager: getLogger ('test ');

/* 6 */if (""! = $ Str ){
$ Logger-> debug ("The str value is not blank! Its value is: ". $ str ."
");
}

/* 7 */if (strlen ($ str)> 4 ){
$ Logger-> debug ("str length is greater than 4! "."
");
}

/* 8 */LoggerManager: shutdown ();
?>
[/Php]

The program is very simple. let's take a look at the functions of each tag:
1. define the LOG4PHP_DIR directory named log4php, which is the log4php directory in our directory structure.
2. including LoggerManager. php. it is the main class we use in the program. it obtains a logger class through its getLogger () static method, and uses this class at 5th.
3. define a string $ str for testing. use the condition judgment in section 6th and section 7th to generate the debug information of logger.
4. here is your PHP output. in order to distinguish the output information of a PHP program from that of a logger, a piece of information is output first.
5. use the getLogger () static method of the LoggerManager class to obtain a logger class. its parameter is generally the class name. This information is used to distinguish different logger records. here we only use one Test class, therefore, we can define this name as test. of course, you can also define it as test1, test2...
6. if $ str is not empty, a debugging message is output.
7. if the length of $ str exceeds 4, a debugging message is output.
OK. Now let's execute http: // localhost/test1.php. no. there are only four output statements without debugging information! What's going on? The reason is that we have not set the log4php configuration file! Well, I have a simple configuration file, the code is as follows:
[Php]
Log4php. rootLogger = DEBUG, A1
Log4php. appender. A1 = LoggerAppenderEcho
Log4php. appender. A1.layout = LoggerLayoutSimple
[/Php]
No matter what the code means, create a new log4php. properties file in the app Directory, copy the above code to the file, and save it.
Note:
1. the file name must be log4php. properties. do not write an error.
2. this file must be the same as index. php, that is, under the app directory.

OK. after performing this step, continue to execute http: // localhost/test1.php,
In this case, we will see the following code:
[Php]
Here is the PHP output, which has nothing to do with log4php!
The DEBUG-str value is not empty! The value is here is test string!
The length of DEBUG-str is greater than 4!
[/Php]
When I see this, someone will say, why are there two points of attention above? I want to change the file name to log4.txt, and put it in another place for convenience. for example, I have a special configuration file directory config/in my project. I want to put log4.txt in the config/Directory. OK, yes, if you do this, you need to add a statement at the end of 1 in test1.php:
[Php]
Define (LOG4PHP_CONFIGURATION, "/config/log4.txt ");
[/Php]
It must be noted here, because the attribute file of log4php adopts the properties format, which has a general extension in JAVA. properties, because the extension of log4.txt is changed to log4.properties, which is more suitable for use. Log4php also supports configuration files defined in XML format. However, XML files are generally less readable. here I will use the configuration files in properties format.

Okay. let's take a look at the advanced stuff below. let's look at the page after execution. it seems very simple. it's no different from echo. well, we don't need to know why first, first, set the last log4php in log4.properties. appender. the value of A1.layout is changed from LoggerLayoutSimple to LoggerLayoutHtml. after saving the value, run http: // localhost/test1.php. haha, are you feeling more comfortable now? I can afford to see this thing for so long. The so-called advanced usage is to describe the usage of this configuration file in detail. Through the configuration file, we can use some more advanced functions, such as writing logs into files.

IV. Advanced use:
Log4php consists of three important components: log information priority, log information output destination, and log information output format. 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]
1. LoggerAppenderConsole uses php: // stdout as the output location
2. LoggerAppenderDailyFile is inherited from LoggerAppenderFile. it outputs a file every day as the output location.
3. LoggerAppenderDb uses the database as the output location
4. LoggerAppenderEcho output at the end of the execution file
5. LoggerAppenderFile uses the file as the output location
6. LoggerAppenderMail uses the email as the output location
7. LoggerAppenderMailEvent inherits from LoggerAppenderMail, which is triggered by an event based on the email output location.
8. LoggerAppenderNull: No information is output.
9. LoggerAppenderPhp: output to PHP error information, convert various log levels to php Standard information
10. LoggerAppenderRollingFile: inherits from LoggerAppenderFile and is output in the form of xxx. log.1, xxx. log.2,
11. LoggerAppenderSocket: output in socket mode
12. LoggerAppenderSyslog: The syslog is output as a system log and recorded using the syslog () function in php.
[/Php]

The configuration file shows the alias "log4php. rootLogger = DEBUG, A1" starting from the second parameter. the specific destination setting method is as follows:
[Php]
Log4php. appender. A1 = LoggerAppenderEcho
Log4php. appender. A1.layout = LoggerLayoutHtml
[/Php]
Each output destination has different set parameters. according to actual needs, there are multiple output destinations, as shown below:
Log4php. rootLogger = DEBUG, A1, A2, A3 ,....
A1, A2, and A3 indicate different output destinations.

3. log file output format. As an attribute of the output destination, log4php contains five different output methods:
[Php]
1. LoggerLayoutHtml: outputs debugging information in html format
2. LoggerLayoutSimple: simple display in the format of "level information-log information"
3. LoggerLayoutTTCC: displayed in the format of "monthly, daily, or yearly [process] Level Information log name-debugging information"
4. LoggerPatternLayout)
5. LoggerXmlLayout: output in xml format
[/Php]

The so-called advanced usage is the use of the configuration file in log4php. all advanced functions must be completed by the configuration file. each time I talk about a function, there is a sample file, you can check the tutorial and debug it. here I will take several common output destinations as an example. for details, see the test directory in the log4php extract directory.

1. LoggerAppenderEcho: definition method:
[Php]
Log4php. appender. A1 = LoggerAppenderEcho
Log4php. appender. A1.layout = LoggerLayoutHtml
[/Php]
Log4php. appender is a fixed format. A1 represents the alias of the output location, log4php. appender. a1 is the output location. here it is the direct Echo, log4php. appender. a1.layout defines the format for direct back-to-Display. The value here can be five output formats. the current setting indicates that HTML is used to output DEBUG information. when layout of A1 is configured as LoggerLayoutHtml, it also has two additional parameters to define (remember: the parameters here refer to setting parameters for LoggerLayoutHmtl not for LoggerAppenderEcho, but for the output format rather than for the output destination ), they are:
1. locationInfo: output local information with the value boolean
2. title: the title name of the output html, that is, the title name in the output HTMLName, string

Complete configuration example:
[Php]
Log4php. rootLogger = DEBUG, A1

Log4php. appender. A1 = LoggerAppenderEcho

Log4php. appender. A1.layout = LoggerLayoutHtml
Log4php. appender. A1.layout. locationInfo = true
Log4php. appender. A1.layout. title = test instance 1
[/Php]
The instance in this section is log_echo.php.


2. LoggerAppenderFile:
Definition format:
[Php]
Log4php. appender. A1 = LoggerAppenderFile
Log4php. appender. A1.layout = LoggerLayoutHtml
[/Php]
Similarly, the output location is File, and the output format is loggerLayoutHtml. when the output destination is LoggerAppenderFile, you need to set several parameters:
1. file: Name of the file to be output, string
2. append: whether to add the record to the end of the file at each time. if the value is boolean, the original data is not cleared when the value is true. if the value is false, the original record is cleared.
The complete setting mode is:
[Php]
Log4php. rootLogger = DEBUG, A1

Log4php. appender. A1 = LoggerAppenderFile
Log4php. appender. A1.file = logs/log4php_file.log.html
Log4php. appender. A1.append = false

Log4php. appender. A1.layout = LoggerLayoutHtml
Log4php. appender. A1.layout. locationInfo = true
Log4php. appender. A1.layout. title = test instance 1
[/Php]
It is clearly displayed that the A1.append here should be set to false because it is output in HTML format. if it is true, the entire HTML information is written every time in the file. However, it is a pity that log4php does not provide good support for Chinese characters. The title of the generated HTML file is correct in Chinese, and all the Chinese characters of the debugging information are garbled. Note this.
The instance in this section is log_file.php.


3. LoggerAppenderDailyFile:
Definition format:
[Php]
Log4php. rootLogger = DEBUG, A1

Log4php. appender. A1 = LoggerAppenderDailyFile

Log4php. appender. A1.layout = LoggerLayoutSimple
[/Php]
Set the value of log4php. appender. A1 to use the time-based log file output, and select the simplest LoggerLayoutSimple output method. when LoggerAppenderDailyFile is selected as the output destination, you need to set the following parameters:
1. file: output file name, which is in the format of "file name _ % s. log". % s at the end indicates the included time. the time definition format is as follows:
2. datePattern: time format, indicating to format the current date as % s in the string-type replacement file name.
Complete setting mode:
[Php]
Log4php. rootLogger = DEBUG, A1

Log4php. appender. A1 = LoggerAppenderDailyFile
Log4php. appender. A1.datePattern = Ymd
Log4php. appender. A1.file = logs/log4php_dailyfile _ % s. log
Log4php. appender. A1.append = true

Log4php. appender. A1.layout = LoggerLayoutSimple
[/Php]
The output format is LoggerLayoutSimple, which indicates that I write data in a simple format such as [level information]-debugging information.
The instance in this section is log_dailyfile.php.

4. LoggerAppenderRollingFile:
Definition format:
[Php]
Log4php. rootLogger = DEBUG, A1

Log4php. appender. A1 = LoggerAppenderRollingFile

Log4php. appender. A1.layout = LoggerLayoutSimple
[/Php]
Indicates that files are arranged by serial number. when the size of the log file exceeds the limit, the old log is backed up and the new log is overwritten.
Let's take a look at its related parameters:
1. file: string type of the file name to be output
2. maxFileSize: the maximum number of log files in the format of k
3. maxBackupIndex: the maximum number of sequences is the log file. The maximum numeric value behind the log file.

The complete configuration file is as follows:
[Php]
Log4php. rootLogger = DEBUG, A1

Log4php. appender. A1 = LoggerAppenderRollingFile
Log4php. appender. A1.file = logs/log4php_rollingfile.log
Log4php. appender. A1.maxFileSize = 5KB
Log4php. appender. A1.maxBackupIndex = 5

Log4php. appender. A1.layout = LoggerLayoutSimple
[/Php]
In this example, the maxFileSize is adjusted to 5 KB. in actual use, set as needed. other optional units are KB, MB, and GB.
The instance in this section is log_rollingfile.php.


5. LoggerPatternLayout output format details:
Here we set the output destination to the simplest LoggerAppenderEcho to see how to use LoggerPatternLayout at the layout layer:
Complete body configuration file:
[Php]
Log4php. rootLogger = DEBUG, A1

Log4php. appender. A1 = LoggerAppenderEcho

Log4php. appender. A1.layout = LoggerPatternLayout
Log4php. appender. A1.layout. conversionPattern = %-5 p % d % t % m in % F line % L % n
[/Php]
The functions of conversionPattern parameters are as follows:
* C: The current logger name. The $ cateName value passed in through LoggerManager: getLogger ($ cateName)
* C: the class name of the current complete body (the class that generates log information, the class name in log4php)
* D: current time
* F: current file name
* L: log generation class
* L: number of lines that generate log information
* N: line feed
* M: debugging information
* M: method name for generating log information
* P: log level information
* R: execution time in ms
* T: the thread that generates log information.
* X: NDC (nested diagnostic context)
* X: MDC (mapped diagnostic context)
Here, there is a bug in version 0.9.% F and % L cannot display the value normally. you only need to modify lines 189 and 190 of log4php \ spi \ LoggerLoggingEvent. php:
[Php]
189 $ className = @ $ hop ['class'];
190 if (! Empty ($ className) and ($ className = 'logger' or get_parent_class ($ className) = 'logger '))
[/Php]
Change to the following:
[Php]
189 $ className = @ strtolower ($ hop ['class']);
190 if (! Empty ($ className) and ($ className = 'logger' or $ className = 'loggercategory 'or get_parent_class ($ className) = 'logger') or get_parent_class ($ className) = 'loggercategory ')
[/Php]
(From: http://ipis007.spaces.live.com/blog/cns! 75F0E4FEE274140B! 1056. entry. the error file in the original file is LoggerLocationInfo. php. it should actually be LoggerLoggingEvent. php)


6. multi-output location settings:
With the above knowledge, the setting of multiple output locations will naturally be clear. let's look at a configuration file:
[Php]
Log4php. rootLogger = DEBUG, A1, A2

Log4php. appender. A1 = LoggerAppenderEcho
Log4php. appender. A1.layout = LoggerPatternLayout
Log4php. appender. A1.layout. conversionPattern = "%-5 p % l % d % c % t % m in % F line % L % n"

Log4php. appender. A2 = LoggerAppenderFile
Log4php. appender. A2.file = logs/log4php_mutil.log
Log4php. appender. A2.append = false
Log4php. appender. A2.layout = LoggerPatternLayout
Log4php. appender. A2.layout. conversionPattern = "%-5 p % l % d % c % t % m in % F line % L % n"
[/Php]
I believe that everyone can understand the above configuration File, and leave it unnecessary. here, the output alias A1 and A2 can be defined by yourself. you can define File, Echo...


Application of log4php in phpsa framework
Phpsa3.0 starts to integrate log4php for debugging. the framework-level debugging information level is INFO. you can use the DEBUG level in the user-defined module to output log information, to understand the framework structure, you can set/comm/config/log4php. log4php in properties. the attribute of rootLogger is set to DEBUG. the default value is DEBUG. the priority values of rootLogger are ERROR, WARN, INFO, and DEBUG, if you do not want to see the debug information output by the system, set the debug level to INFO. in the module file, use $ this-> logger-> info ("xxxx") to output info information.
Most classes in the phpsa framework add deub () log information to key methods to record the execution sequence of the framework. initialize logger in the Object class and call it in its inheritance class. the framework uses config: IS_LOG to record whether log4php is used. in actual development, we can set the value of this variable to true for log output, set this value to false during release to achieve better performance.
In the Object constructor, the if statement is also used to load and initialize the class library:
[Php]
If ($ this-> isLog ){
Require_once ($ _ SERVER ['document _ root']. "/comm/config/phpsaLogger. inc. php ");
$ This-> logger = LoggerManager: getLogger ($ this-> toString ());
}
[/Php]
In the user-defined module, we can perform initialization like this:
If (Config: IS_LOG ){
$ This-> logger = LoggerManager: getLogger ($ this-> toString ());
}
Call this method as follows:
If (Config: IS_LOG ){
$ This-> logger-> info ("here is debug info ...");
}

?

?

/*
// Const LOG4PHP = true;
// Const LOG4PHP_DEFAULT_INIT_OVERRIDE = true;
// Const LOG4PHP_CONFIGURATION_FILE = '';
Define (LOG4PHP_DIR, LIB_PATH. 'log4php-0.9/src/log4php ');
Define (LOG4PHP_CONFIGURATION, C_PATH. 'log4php. win. properties ');
Require_once (LOG4PHP_DIR. '/LoggerManager. php ');

$ Logger = LoggerManager: getLogger ($ category );
$ Logger-> $ level ($ msg );
*/

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.