Detailed configuration example description for log4php0.9
First, what is log4php:
Log4j in Java can be regarded as the famous log development package, it for the Apche organization maintenance project, VXR Brother use PHP to achieve the log4j function, currently log4php as a sub-project log4j exist, more information can be clicked:/http logging.apache.org/log4php/to view, the other VXR brother's log4php official station is: http://www.vxr.it/log4php/, interested friends can directly click on the top two URLs to view the details.
Download Address: http://www.vxr.it/log4php/download.html
Second, installation:
The download version of this article is log4php-0.9.tar.gz, the extracted directory has the SRC directory, the {Unzip directory}/src/log4php/directory to your project directory, complete the installation. Because this article is used to explain the log4php, so not related directories are not listed, it is defined as http://localhost, the project structure is as follows:
+app/
+ log4php
+ images/
+ logs/
+ js/
+ css/
-Log4php.properties
-index.php
Third, start using:
First of all, let's take a look at the contents of test1.php:
[PHP]
/*1*/define (Log4php_dir, "log4php");
/*2*/require_once (Log4php_dir. '/loggermanager.php ');
/*3*/$str = "Here is Test string!";
/*4*/echo "Here is the output of PHP, nothing to do with log4php yo!"
";
/*5*/$logger = Loggermanager::getlogger (' test ');
/*6*/if (""! = $str) {
$logger->debug ("The value of STR is not empty! Its value is: ". $str. "
");
}
/*7*/if (strlen ($STR) > 4) {
$logger->debug ("STR is longer than 4!". "
");
}
/*8*/Loggermanager::shutdown ();
?>
[/php]
The
Program is very simple to look at the role of the various tags:
1. Define the Log4php_dir directory named log4php, which is the log4php directory in our directory structure
2. Contains loggermanager.php, It is the class we use primarily in our program, and it uses the GetLogger () static method to obtain a logger class, which is used in the 5th place with the class
3. Defines a string $str, which is used to test and use conditional judgments at 6th and 7th to produce the debug information of logger.
4. This represents your PHP output, in order to distinguish between the PHP program output and logger output information is different, here first output a message.
5. A logger class is obtained by using the GetLogger () static method of the Loggermanager class, whose arguments are typically class names, which are used to distinguish different logger records, where we only use a test class, So this name we can define as test, of course you can also define as Test1, test2 ...
6. If $str is not empty, output a debug message
7. If $STR is longer than 4 output a debug message
OK, now let's execute http://localhost/test1.php. No, There are only 4 output statements without debugging information! What's going on? The reason is that we haven't set up the log4php profile yet! OK, I have a simple configuration file here, the code is as follows:
[PHP]
Log4php.rootlogger=debug, A1
Log4php.appender.a1=loggerappenderecho
Log4php.appender.a1.layout=loggerlayoutsimple
[/php]
Don't care what these codes mean, Create a new Log4php.properties file in the app directory, and then copy the above code to the file and save it.
Note:
1. The filename of this file must be log4php.properties, do not write the error
2. This file needs to be in the same class as index.php, which is in the app directory
OK, perform this step to proceed with http://localhost/test1.php,
At this point we will see this code:
[PHP]
Here is the output of PHP, not related to log4php yo!
The value of DEBUG-STR is not empty! It has the value: here is Test string!
The length of the DEBUG-STR is greater than 4!
[/php]
See here, someone will say, why the top there will be two attention to the place? I want to change the file name to Log4.txt, at the same time in order to facilitate me to put its location in another place, such as my project has a special profile directory config/, I want to put Log4.txt in the config/directory, OK, yes, if you do, Then in the test1.php you need to add a statement to the 1 place:
[PHP]
Define (log4php_configuration, "/config/log4.txt");
[/php]
It must be explained here, because the Log4php property file is in the properties format, which in Java generally has a. properties extension, so we'll still modify the log4.txt extension to Log4.properties, This is more in line with the habit. Log4php also supports configuration files defined in XML format, but in general the readability of XML files is poor, and I use the properties format configuration file uniformly here.
OK, high-level things we put down to see, we look at the implementation of the page, as if very simple, and echo is no different, OK, we don't need to know why, Change the value of the last log4php.appender.A1.layout in Log4.properties from Loggerlayoutsimple to loggerlayouthtml, and then execute http://After saving localhost/test1.php look, haha, now is not feeling more comfortable? Also worthy of this oneself spent so long time to see this thing. Behind the so-called advanced use is to elaborate on the use of this configuration file. With configuration files, we can use some of the more advanced features, such as writing a log into a file.
Iv.. Advanced use:
Log4php consists of three important components: the priority of the log information, the output destination of the log information, and the output format of the log information. The priority of log information is from high to low with error, WARN, INFO, DEBUG, respectively, to specify the importance of this log information; the output destination of the log information specifies whether the log will be appended to the end of the file or the file, and the output format controls the display of the log information.
1. The priority of the log, the log information has four categories (commonly used), you can set the priority in the configuration file to show or hide debugging information in the code, for example, when I adjust the log level to warn level, then the error message in the program (through $log-> Error ("error message here!") ) and warn level information can be recorded, and all information in the program using info and debug level will not be displayed, it is
The first parameter definition in "Log4php.rootlogger=debug, A1", which means that the debug level is defined as the debug level, that is, all the error in the program, Warn,info, Debug information can be displayed, if the level is set to info, then there can be only error, WARN, info Three kinds of information can be displayed.
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.
FATAL level indicates that each serious error event will cause the application to exit.
2. The output destination of the log information. 12 types of output destinations are supported in log4php:
[PHP]
1. The loggerappenderconsole takes php://stdout as the output
2. Loggerappenderdailyfile inherits from Loggerappenderfile and outputs a file
3 per day as a file output. Loggerappenderdb the database as the output
4. Loggerappenderecho output
5 at the end of the execution file. Loggerappenderfile the file as the output
6. Loggerappendermail the message as the output
7. Loggerappendermailevent inherits from Loggerappendermail, with the message as the output, triggering
8 for the event. Loggerappendernull: Do not output any information
9. loggerappenderphp: Output to PHP error message, convert various log level information to PHP standard Information
10. Loggerappenderrollingfile: Inherit from Loggerappenderfile, output in Xxx.log.1, xxx.log.2 form,
11. Loggerappendersocket: Output
12 as socket. Loggerappendersyslog: The system log is the destination output, using the Syslog () function in PHP to record
[/php]
The
is represented in the configuration file as "Log4php.rootlogger=debug, A1" aliases from the second parameter, where the destination is set by:
[PHP]
log4php.appender.a1= Loggerappenderecho
log4php.appender.a1.layout=loggerlayouthtml
[/php]
to implement, each output destination has a different set of parameters, according to actual needs, The destination of the output can be a number of items, as follows:
Log4php.rootlogger=debug, A1, A2, A3, ....
where A1, A2, A3 represent different output destinations.
3. log file output format. It exists as an attribute of the output destination, and log4php contains 5 different output modes:
[PHP]
1. loggerlayouthtml: Output debug information in HTML format
2. Loggerlayoutsimple: A simple display of
3 in the format "level information-log information". LOGGERLAYOUTTTCC: Displays
4 in the format "month/day/year time [process] level information Log name-debug information". Loggerpatternlayout: The pattern expression as the output format to display (This function is very powerful, you can do whatever you want to set the output format, explained in detail in the Example section)
5. Loggerxmllayout: Output
[/php]
in XML mode
Here the so-called advanced use is the use of the configuration files in the log4php, all the advanced features to be done by the configuration file, I use each of the functions, there is a sample file, you can watch the tutorial side for debugging, here I will be a few common output destinations as an example to explain, For details, you can view the test directory in the log4php decompression directory.
1. Loggerappenderecho: Define the way:
[PHP]
Log4php.appender.a1=loggerappenderecho
Log4php.appender.a1.layout=loggerlayouthtml
[/php]
Log4php.appender is a fixed format, A1 represents the output of the alias, log4php.appender.A1 for the output, here is the direct echo, log4php.appender.A1.layout define the format of the direct echo, where the value can be 5 output format , the current settings represent the use of HTML to output debug information, and when A1 's layout is configured as loggerlayouthtml, it has two additional parameters that can be defined ( Keep in mind that the parameters here refer to the LOGGERLAYOUTHMTL set to not Loggerappenderecho, which sets the parameters for the output format instead of setting the parameters for the output destination, respectively:
1. Locationinfo: output local information with a value of Boolean
2. Title: The header name of the output HTML, which is the output HTMLThe name in the string
The complete configuration example is as follows:
[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= Here is the test example 1
[/php]
The example in this section is log_echo.php.
2. Loggerappenderfile:
Define the format:
[PHP]
Log4php.appender.a1=loggerappenderfile
Log4php.appender.a1.layout=loggerlayouthtml
[/php]
Again, this means that the output is file and the output format is loggerlayouthtml. Several parameters are also set when the output destination is loggerappenderfile:
1. File: filename to Output, string
2. Append: If each record is added to the end of the file, the value is Boolean, True when the original data is not purged, false when the original record is cleared.
The complete Setup 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= Here is the test example 1
[/php]
It is clear that the a1.append here should be set to false because it is an HTML output, and if true, the entire HTML information will be written to the file each time. However, there is a regret that log4php support for Chinese is not very good, the HTML file generated by the title of Chinese correct, debugging information in Chinese is garbled. This point of attention.
The example in this section is log_file.php.
3. Loggerappenderdailyfile:
Define the format:
[PHP]
Log4php.rootlogger=debug, A1
Log4php.appender.a1=loggerappenderdailyfile
Log4php.appender.a1.layout=loggerlayoutsimple
[/php]
By setting the value of the log4php.appender.A1, use the log file to output by time, and the output mode here selects the simplest loggerlayoutsimple. When selecting Loggerappenderdailyfile as the output destination, you need to set the following parameters:
1. File: Output file name, in the form of "file name _%s.log" to output, the trailing%s represents the time, the time is defined as the following parameters
2. Datepattern: Time format, which represents the%s in the format of the current date as a string substitution file name.
The full setup mode is:
[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 of loggerlayoutsimple means that I write in a format that is simple "[Level information]-debug information."
The example in this section is log_dailyfile.php
4. Loggerappenderrollingfile:
Define the format:
[PHP]
Log4php.rootlogger=debug, A1
Log4php.appender.a1=loggerappenderrollingfile
Log4php.appender.a1.layout=loggerlayoutsimple
[/php]
Indicates that files are arranged by serial number, backup old logs and rewrite new logs when log file size exceeds maximum definition
Take a look at its related parameters:
1. File: String type of filename to output
2. maxfilesize: Log file Maximum number of bytes in the format: value K
3. Maxbackupindex: The maximum number of sequences is the log file. The maximum numeric value of the rear.
After the overall 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]
Here for the demo to adjust the maxfilesize to 5KB, the actual use of the settings as required. Other optional units are KB,MB,GB.
The example in this section is log_rollingfile.php.
5. Loggerpatternlayout Output Format Detailed:
Here our output destination is set to the simplest loggerappenderecho, to see how to use Loggerpatternlayout in the layout layer:
Complete configuration file:
[PHP]
Log4php.rootlogger=debug, A1
Log4php.appender.a1=loggerappenderecho
Log4php.appender.a1.layout=loggerpatternlayout
log4php.appender.a1.layout.conversionpattern=%-5p%d%t%m in%F line%l%n
[/php]
The functions of the parameters in Conversionpattern are as follows:
* C: Current logger name, $catename value passed through Loggermanager::getlogger ($cateName)
* C: The current complete class name (the class that generated the log information, the class name in log4php)
* D: Current time
* F: Current file name
* L: Class for generating log information
* L: Number of rows generating log information
* N: Line break
* M: Debug information
* M: Method name for generating log information
* P: Log level information
* r: Time of execution ms
* t: The thread that generated the log information
* X:NDC (nested diagnostic context)
* X:MDC (mapped diagnostic context)
There is a bug in the 0.9 version,%f and%l cannot display the values correctly, only the log4php\spi\loggerloggingevent.php 189 and 190 rows need to be modified:
[PHP]
189 $className = @ $hop [' class '];
if (!empty ($className) and ($className = = ' Logger ' or get_parent_class ($className) = = ' Logger '))
[/php]
Change to the following:
[PHP]
189 $className = @strtolower ($hop [' class ']);
!empty ($className) and ($className = = ' Logger ' or $className = = ' Loggercategory ' or Get_parent_class ($className) = = ' logger ') or get_parent_class ($className) = = ' Loggercategory ')
[/php]
(Excerpt from: Http://ipis007.spaces.live.com/blog/cns!75F0E4FEE274140B!1056.entry, the original error file is loggerlocationinfo.php, The actual should be loggerloggingevent.php)
6. Multi-Output settings:
With the above to know, the settings of the multi-output will naturally understand, to see a configuration file:
[PHP]
Log4php.rootlogger=debug, A1, A2
Log4php.appender.a1=loggerappenderecho
Log4php.appender.a1.layout=loggerpatternlayout
Log4php.appender.a1.layout.conversionpattern= "%-5p%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= "%-5p%l%d%c%t%m in%F line%l%n"
[/php]
I believe you can understand the above configuration file, the redundant will not say, here the output alias A1, A2 You can define, you can define file, Echo ...
Application of five log4php in PHPSA frame
PHPSA3.0 began to integrate log4php for debugging, the framework level of debug information level is info, user-defined module can use the debug level to output log information, want to understand the structure of the framework can be/comm/config/ The properties of the Log4php.rootlogger in Log4php.properties are set to debug, the default is the debug level, and it is necessary to specify that their priority levels are error, WARN, INFO, Debug, respectively, If you do not want to see debug information for the system output, set the debug level to info and use $this->logger->info ("XXXX") in the module file to output info.
Most classes in the PHPSA framework include DEUB () log information in key methods to record the order in which the framework is executed. Logger is initialized in the object class and is called in its inheriting class. The framework uses Config::is_log to record whether or not to use log4php, and in actual development we can set the value of this variable to true for log output, and set this value to False for better performance when publishing.
The IF statement is also used in the constructor of object to load and initialize class libraries:
[PHP]
if ($this->islog) {
Require_once ($_server[' document_root '). "/comm/config/phpsalogger.inc.php");
$this->logger = Loggermanager::getlogger ($this->tostring ());
}
[/php]
We can initialize this in a user-defined module:
if (Config::is_log) {
$this->logger = Loggermanager::getlogger ($this->tostring ());
}
This is called:
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);
*/