log4j Pattern Layout Conversionpattern Detailed

Source: Internet
Author: User
Tags print format log4j

Spring uses log4j, and there are 2 ways to do it.

1. Do not make any configuration in Web. Xml.

Log4j.properties placed in the Classpath root directory,

The resulting log file does not have a relative path, and if a relative path is written, it is generated under the root path of the installation tomcat.


2. Set in Web. Xml.



<context-param>          <param-name> log4jconfiglocation</param-name>          < param-value>web-inf/classes/log4j.properties</param-value>  </context-param>     <context-param>          < param-name>webapprootkey</param-name>          < param-value>myappfuse.root</param-value>  </context-param>         <listener>          < listener-class>org.springframework.web.util.log4jconfiglistener</listener-class>  </ listener>  <listener>          < Listener-class>org.springframework.web.context.contextlOaderlistener</listener-class>  </listener> 



Printing parameters: log4j Format the log information in a print format similar to the printf function in C, as follows:


%p: Output log information priority, i.e. Debug,info,warn,error,fatal,


Log4j.rootlogger = Info,stdout The INFO in this sentence is set to output the log above this level


Messages such as Info,warn,error,fatal will be output. What is the level of each message? %p is the level at which the message is output.


%d: the date or time of the output log time, the default format is ISO8601, can also be specified after the format, for example:%d{yyyy-mm-dd hh:mm:ss,sss}, output similar to: 2011-10-18 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.
%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, the characters are cut off





This article is from the "Nothing-skywalker" blog, please be sure to keep this source http://tianxingzhe.blog.51cto.com/3390077/1664101

log4j Pattern Layout Conversionpattern Detailed

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.