Configuration of the log4j

Source: Internet
Author: User

For Java Framework class projects such as SSI and SSH

1. Copy the Log4j.jar to the Web-inf/lib directory

2. Create a log4j.properties configuration file under the source folder in the project to configure the log information

3. The detailed settings in Web. Xml are as follows:

   
   
The container scans the log4j configuration file every 60 seconds--
   
   
<!--need to add a Spring-web.jar package, otherwise use an error message--
   


4, log4j configuration detailed description

Log4j.rootcategory=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. Grades can be divided into

Off, FATAL, error, WARN, info, DEBUG, all, if configuration off does not play any information, if configured as info so only display info, WARN, error

Log information, and the debug information is not displayed, refer to the third section to define the logger in the configuration file.

3 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)

Refer to the third section to define the Appender in the configuration file.

4 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 of log information and the information string),
Org.apache.log4j.TTCCLayout (contains information about the time, thread, category, etc.) of the log

Refer to the third section to define layout in the configuration file.

5 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:

%p: Output log information priority, i.e. Debug,info,warn,error,fatal,
%d: the date or time of the output log time, the default format is ISO8601, can also be specified after the format, such as:%d{yyy MMM dd hh:mm:ss,sss}, output similar: October 18, 2002 22:10:28,921
%r: The number of milliseconds to output the log information from the application boot to output
%c: The class in which the output log information 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
%l: The location of the output log event, which corresponds to the combination of%c.%m (%f:%l), including the class name, the thread that occurred, and the number of rows in the code. Example: Testlog4.main (testlog4.java:10)
%x: The NDC (nested diagnostic environment) associated with the output and current line threads, especially for multi-client multithreaded applications such as Java Servlets.
Percent: Output a "%" character
%F: The name of the file where the output log message was generated
%l: Line numbers in the output code
%M: The specified message in the output code, resulting in the log specific information
%n: Output a carriage return line break, Windows platform is "\ r \ n", Unix platform for "\ n" Output log information line-wrapping
You can add modifiers between% and pattern characters to control their minimum width, maximum width, and text alignment. Such as:
1)%20c: Specify the name of the output category, the minimum width is 20, if the category name is less than 20, the default is the right alignment.
2)%-20c: Specify the name of the output category, the minimum width is 20, if the category name is less than 20, the "-" number specifies left-aligned.
3)%.30c: Specify the name of the output category, the maximum width is 30, if the category name is greater than 30, will be the left more than the character of the cut off, but less than 30, there will be no spaces.
4)%20.30c: If the category name is less than 20, fill in the blanks, and right-aligned, if its name is longer than 30 characters, it is exported from the left hand-sold characters are truncated.

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

The output information

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

Refer to the third section to define the formatting log information in the configuration file.

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

This sentence is the same as the 3rd line. The type that defines the output that is named R produces a log file every day. 8 log4j.appender.r.file=d: \\Tomcat 5.5 \\logs\\qc.log

The name of the file named R is defined 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.

Ten 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 are rated as Debug.

can put Com.neusoft Change the package name that you used 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.

Configuration of the log4j

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.