--Application Chapter-----------------------------------------------------------------------------
First, download: http://logging.apache.org/
Second, the application:
1. add Log4j-1.2.8.jar to the Classpath
2, establish log4j.propertiesunder the CLASSPATH . The contents are as follows :
Log4j.rootlogger=error,stdout,fileout log4j.appender.stdout= org.apache.log4j.consoleappenderlog4j.appender.stdout.layout= org.apache.log4j.patternlayoutlog4j.appender.stdout.layout.conversionpattern=%d (%F:%L)%-5p%c-%m% nlog4j.appender.fileout=org.apache.log4j.dailyrollingfileappenderlog4j.appender.fileout.encoding= utf-8log4j.appender.fileout.file=/www/applog/photo/app.loglog4j.appender.fileout.layout= org.apache.log4j.patternlayoutlog4j.appender.fileout.layout.conversionpattern=%d [%t] (%F:%L)%-5p%c-%m%n
3. Call: Add declaration and output log information code in the class to output the log (for example:Test)
private static final Logger Logger = Logger.getlogger (Test.class);
logger.error ("error message");
--Detailed article-----------------------------------------------------------------------------
First,log4j.properties Detailed
1, Log4j.rootlogger=info, stdout, R
This sentence is the output of the log information of level info to stdout and R, the definition of stdout and r in the following code, can be arbitrarily named. Level can be divided into off, FATAL, error, WARN, INFO, DEBUG, all, if the configuration off does not play any information, if configured as info so only display info, WARN, error log information, and DEBUG information will not be displayed.
2, Log4j.appender.stdout=org.apache.log4j.consoleappender
This sentence defines what type of output is named stdout, which can be
Org.apache.log4j.ConsoleAppender (console),
Org.apache.log4j.FileAppender (file),
Org.apache.log4j.DailyRollingFileAppender (generates a log file every day),
Org.apache.log4j.RollingFileAppender (creates a new file when the file size reaches the specified size)
Org.apache.log4j.WriterAppender (send log information in stream format to any specified location)
3, Log4j.appender.stdout.layout=org.apache.log4j.patternlayout
This sentence is the type of layout that defines the output named StdOut, which can be
Org.apache.log4j.HTMLLayout (Layout in HTML table Form),
Org.apache.log4j.PatternLayout (flexibility to specify layout mode),
Org.apache.log4j.SimpleLayout (contains the level and information of the log information),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log
4, log4j.appender.stdout.layout.conversionpattern= [QC]%p [%t]%c.%m (%l) | %m%n
If you use the pattern layout to specify the exact format of the printed information Conversionpattern, print the parameters as follows:
%M the message specified in the output code
%p output priority, i.e. Debug,info,warn,error,fatal
%r output the number of milliseconds that the log information is consumed from the application boot to output
%c output belongs to the class, which is usually the full name of the class
%t output The name of the thread that generated the log event
%n output a carriage return newline character, Windows platform is "RN", UNIX platform is "n"
%d the date or time of the output log time, the default format is ISO8601, or the format can be specified later,
For example:%d{yyyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921
%l where the output log event occurs, including the class name, the thread that occurred, and the number of rows in the code.
[QC] is the beginning of the log information, can be any character, generally referred to as the project abbreviation.
5, log4j.logger.com. Neusoft =debug
Specifies that all classes under the Com.neusoft package are rated as Debug. You can change the Com.neusoft to the package name that you use for your project.
Log4j.logger.com.opensymphony.oscache=error
Log4j.logger.net.sf.navigator=error
These two sentences are the error level of the two packages, and if Ehcache is not configured in the project, these two sentences are not required.
Log4j.logger.org.apache.commons=error
Log4j.logger.org.apache.struts=warn
These two sentences are struts's packages.
Log4j.logger.org.displaytag=error
This sentence is Displaytag's bag. (used by the QC Problem List page)
Log4j.logger.org.springframework=debug
This sentence is spring's package.
Log4j.logger.org.hibernate.ps.preparedstatementcache=warn
Log4j.logger.org.hibernate=debug
These two sentences are Hibernate's package.
The above package settings can be customized according to the actual situation of the project.
Log4j.properties Configuration Detailed