Log4j-1.2.16.ja environment: myeclipse10 + log4j1.2.16 + jdk1.6
1. Create a Java project named log4jtest.
2. SetLog4j-1.2.16.jar copy to project root directory, right-click Project, properties, Java build path, add jars, select
Log4j-1.2.16.jar
3. InSource codeDirectory to create an XML file: log4j. xml. The configuration starts. The content is as follows:
<? XML version = "1.0" encoding = "UTF-8" ?>
<! Doctype log4j: Configuration System "log4j. DTD" >
< Log4j: Configuration Xmlns: log4j = 'HTTP: // jakarta.apache.org/log4j /' >
< Appender Name = "Myfile" Class = "Org. Apache. log4j. rollingfileappender" >
< Param Name = "File" Value = "Logs. log" /> <! -- Set log output file name -->
<! -- Set whether to add new logs to the base of the original log when the service is restarted -->
< Param Name = "APPEND" Value = "True" />
< Param Name = "Maxbackupindex" Value = "10" />
< Layout Class = "Org. Apache. log4j. patternlayout" >
< Param Name = "Conversionpattern" Value = "% D {yyyy-mm-dd hh: mm: Ss. Sss} % 5l [% 5 p]-% m % N" />
</ Layout >
</ Appender >
<! -- Specify logger settings. additivity indicates whether the default Inheritance Mechanism is followed. -->
< Logger Name = "Com. Tsinghua. Test. log4jtest" Additi.pdf = "False" >
< Priority Value = "All" />
< Level Value = "All" />
< Appender-ref Ref = "Myfile" />
</ Logger >
<! -- Root logger settings -->
< Root >
< Appender-ref Ref = "Myfile" />
</ Root >
</ Log4j: Configuration >
4. InCode(Log4jtest. Java) to write logs to the log file. The Code is as follows:
/*
* @ Author sixi
* @ Version 0.1
* Company: Tsinghua
* Date: 2012-09-17
* Description: This is a class used to test log4j.
**/
PackageCom. Tsinghua. test;
ImportOrg. Apache. log4j .*;
Public ClassLog4jtest {
Private Static Logger logger = logmanager. getlogger (log4jtest. Class . Getname ());
/**
* @ Param ARGs
*/
Public Static Void Main (string [] ARGs ){
/* Set log4j to start in the code */
/* The configuration in the XML configuration file has the same effect as the configuration in the code below, */
// Patternlayout P = new patternlayout ();
//
// P. setconversionpattern ("% d {yyyy-mm-dd hh: mm: Ss. Sss} % 5l [% 5 p]-% m % N ");
// Fileappender appender = NULL;
// Try {
// Appender = new fileappender (P, "logs. log", true );
// }
// Catch (exception E)
// {
// E. printstacktrace ();
// }
// Logger. setlevel (level. All );
// Logger. addappender (appender );
/* Set log4j end in the code */
Logger.info ("current time:" + New Java. util. Date (). tostring ());
Logger. Warn ("the neck is too tired to go out for activities ");
Logger. Error (" Program Error. ");
Logger. Trace ("trace information ");
}
}
Source code:
Log4jtest.7z
PS: there is still a problem with the code insertion function of the blog garden.