Log4j-java log file Log4j.properties configuration detailed

Source: Internet
Author: User
Tags html form log4j

First, log4j configuration

The first step: Join Log4j-1.2.8.jar to Lib.

Step two: Establish the log4j.properties under the classpath. The contents are as follows:


If you put it under SRC, you don't have to configure it or you have to go to Web.xml to configure a listener
Reference:
Log4j.rootcategory=info, stdout, R log4j.appender.stdout=org.apache.log4j.consoleappender log4j.appender.stdout . Layout=org.apache.log4j.patternlayout LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=[QC]%p [%t]%C.%M (%L) | %m%n Log4j.appender.r=org.apache.log4j.dailyrollingfileappender log4j.appender.r.file=d:\\tomcat 5.5\\LOGS\\QC. Log log4j.appender.r.layout=org.apache.log4j.patternlayout 1log4j.appender.r.layout.conversionpattern=%d-[ts]%p% T%c-%m%n log4j.logger.com.neusoft=debug log4j.logger.com.opensymphony.oscache=error log4j.logger.net.sf.na Vigator=error Log4j.logger.org.apache.commons=error Log4j.logger.org.apache.struts=warn Log4j.logger.org.displayt Ag=error Log4j.logger.org.springframework=debug Log4j.logger.com.ibatis.db=warn log4j.logger.org.apache.velocity= FATAL Log4j.logger.com.canoo.webtest=warn Log4j.logger.org.hibernate.ps.preparedstatementcache=warn log4j . Logger.org.hibernate=debug Log4j.logger.org.logicalcobwebs=warn Log4j.rootcategory=info, stdout, R log4j.appender.stdout=
Org.apache.log4j.ConsoleAppender Log4j.appender.stdout.layout=org.apache.log4j.patternlayout LOG4J.APPENDER.STDOUT.LAYOUT.CONVERSIONPATTERN=[QC]%p [%t]%c.%m (%l) | %m%n Log4j.appender.r=org.apache.log4j.dailyrollingfileappender Log4j.appender.r.file=d:\\tomcat 5.5\\logs\\ Qc.log Log4j.appender.r.layout=org.apache.log4j.patternlayout 1log4j.appender.r.layout.conversionpattern=%d-[ts]
%p%t%c-%m%n log4j.logger.com.neusoft=debug log4j.logger.com.opensymphony.oscache=error
Log4j.logger.net.sf.navigator=error Log4j.logger.org.apache.commons=error Log4j.logger.org.apache.struts=warn
Log4j.logger.org.displaytag=error Log4j.logger.org.springframework=debug Log4j.logger.com.ibatis.db=warn

Log4j.logger.org.apache.velocity=fatal Log4j.logger.com.canoo.webtest=warn
Log4j.logger.org.hibernate.ps.preparedstatementcache=warn Log4j.logger.org.hibernate=debug  Log4j.logger.org.logicalcobwebs=warn

The third step: the corresponding modification of the attributes, before the change must know what these are doing, in the second part of the explanation.

Step Fourth: Add the relevant statement to the class where you want to output the log:

Define attributes: Protected final log = Logfactory.getlog (GetClass ());

In the appropriate method:

if (log.isdebugenabled ()) {

Log.debug ("System ...");

}

Ii. Description of Log4j

1 Og4j.rootcategory=info, stdout, R

This sentence is to the level of info log information output to stdout and r these two destinations, stdout and R definition in the following code, you can arbitrarily name. Levels can be divided into off, FATAL, error, WARN, INFO, DEBUG, and all, if configured off, do not play any information, if configured as info so only info, WARN, error log information, and DEBUG information will not be displayed, The specific explanation can refer to the third part to define the logger in the configuration file.

3 Log4j.appender.stdout=org.apache.log4j.consoleappender

This sentence defines the type of output that is named StdOut, which can be

Org.apache.log4j.ConsoleAppender (console),

Org.apache.log4j.FileAppender (file),

Org.apache.log4j.DailyRollingFileAppender (a log file is generated every day),

Org.apache.log4j.RollingFileAppender (a new file is generated when the file size reaches a specified size)

Org.apache.log4j.WriterAppender (send log information to any specified place in streaming format)

The specific explanation can refer to the third part to define the Appender in the configuration file.

4 Log4j.appender.stdout.layout=org.apache.log4j.patternlayout

This is the type of layout that defines the output end of the name stdout, which can be

Org.apache.log4j.HTMLLayout (layout in HTML form),

Org.apache.log4j.PatternLayout (You can specify layout patterns flexibly),

Org.apache.log4j.SimpleLayout (The level and information string that contains the log information),

Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, and so on that the log was generated)

The specific explanation can refer to the third part to define the layout in the configuration file.

5 log4j.appender.stdout.layout.conversionpattern= [QC]%p [%t]%c.%m (%l) | %m%n

If you use pattern layout to specify the specific format of the printed information Conversionpattern, the printing parameters are 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 it takes to boot to output the log information

The class to which the%c output belongs, usually the full name of the class in which it is located

%t output The name of the thread that generated the log event

%n output a carriage return line feed, Windows platform "RN", UNIX platform "n"

%d output log point-in-time date or time, the default format is ISO8601, you can also specify the format after, such as:%d{yyyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921

%l the location where the output log event occurs, including the class name, the thread that occurred, and the number of lines in the code.

[QC] is the beginning of log information, can be any character, generally for the project abbreviation.

Information for output

[TS] DEBUG [main] Abstractbeanfactory.getbean (189) | Returning cached instance of singleton Bean ' myautoproxy '

You can define the format log information in the configuration file by reference to the third part.

7 Log4j.appender.r=org.apache.log4j.dailyrollingfileappender

This sentence is the same as line 3rd. The type that defines the output port named R generates a log file per day.

8 Log4j.appender.r.file=d:\\tomcat 5.5\\logs\\qc.log

This sentence defines the file name of the output end named R as D:\\tomcat 5.5\\logs\\qc.log can be modified by itself.

9 Log4j.appender.r.layout=org.apache.log4j.patternlayout

Same as Line 4th.

Log4j.appender.r.layout.conversionpattern=%d-[ts]%p%t%c-%m%n

Same as Line 5th.

Log4j.logger.com. Neusoft =debug

Specifies that all classes under the Com.neusoft package have a level of debug.

You can change the Com.neusoft to the name of the package you used for your project.

Log4j.logger.com.opensymphony.oscache=error

Log4j.logger.net.sf.navigator=error

These two sentences are to set the error level of the two packages to be errors, if there is no configuration Ehcache in the project, you do not need these two sentences.

Log4j.logger.org.apache.commons=error

Log4j.logger.org.apache.struts=warn

These two sentences are struts ' packages.

Log4j.logger.org.displaytag=error

This is Displaytag's bag. (For the QC Issues List page)

Log4j.logger.org.springframework=debug

This sentence is a spring package.

Log4j.logger.org.hibernate.ps.preparedstatementcache=warn

Log4j.logger.org.hibernate=debug

These two sentences are Hibernate's bag.

These package settings can be customized according to the actual situation of the project.



Reprint: http://www.cnblogs.com/pigtail/archive/2013/02/16/2913195.html

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.