A flexible layout configurable with pattern string. The goal of this class is to format a LogEvent and return the results. The format of the result depends on theconversion pattern.
The conversion pattern is closely related to the conversion pattern of the printf function in C. A conversion pattern is composed of literal text and format control expressions calledconversion specifiers.
Note that any literal text may be included in the conversion pattern.
Each conversion specifier starts with a percent sign (%) and is followed by optionalformat modifiers and a conversion character. The conversion character specifies the type of data, e.g. category, priority, date, thread name. The format modifiers control such things as field width, padding, left and right justification. The following is a simple example.
Let the conversion pattern be "%-5p [%t]: %m%n" and assume that the Log4j environment was set to use a PatternLayout. Then the statements
Logger logger = LogManager.getLogger("MyLogger"); logger.debug("Message 1"); logger.warn("Message 2");
DEBUG [main]: Message 1 WARN [main]: Message 2
Note that there is no explicit separator between text and conversion specifiers. The pattern parser knows when it has reached the end of a conversion specifier when it reads a conversion character. In the example above the conversion specifier%-5p means the priority of the logging event should be left justified to a width of five characters.
类似于上一篇文章中,
<PatternLayout pattern="%d{HH:mm:ss.SSS} [%t : %L] %-5level %logger{36} - [example] %msg%n"/>
运行时log如下:
11:29:41.730 [main: 16] INFO HelloWorld - [example] Hello, World!
11:29:41.732 [main: 17] ERROR HelloWorld - [example] Hello, World!
由此可见:
下面是pattern中,位于%后面的字符。
%t 或者%thread, 表示Outputs the name of the thread that generated the logging event. 打印这个log的线程的名字。
M/method: Outputs the method name where the logging request was issued.
%L 表示java 文件的行号
%n 表示换行,windows和linux不同上代表的字符不一样。
%d表示输出date,而用大括号括起来的是日期的格式,日期格式如下:
d{pattern}
date{pattern}
Outputs the date of the logging event. The date conversion specifier may be followed by a set of braces containing a
date and time pattern string per SimpleDateFormat.
The predefined formats are ABSOLUTE, COMPACT, DATE, ISO8601, and ISO8601_BASIC.
You can also use a set of braces containing a time zone id per java.util.TimeZone.getTimeZone.
If no date format specifier is given then ISO8601 format is assumed.
Pattern Example
%d{ISO8601} 2012-11-02 14:34:02,781
%d{ISO8601_BASIC} 20121102 143402,781
%d{ABSOLUTE} 14:34:02,781
%d{DATE} 02 Nov 2012 14:34:02,781
%d{COMPACT} 20121102143402781
%d{HH:mm:ss,SSS} 14:34:02,781
%d{dd MMM yyyy HH:mm:ss,SSS} 02 Nov 2012 14:34:02,781
%d{HH:mm:ss}{GMT+0} 18:34:02,781
Location Information
If one of the layouts isconfigured with a location-related attribute likeHTML locationInfo,or one of the patterns%C or $class,%F or %file,%l or %location,%L or %line,%M or %method,Log4j will take a snapshot of thestack, and walk the stack trace to find the location information.
例如:
<PatternLayout pattern="%d [%t - %F:%M:%L] %l - [example] %msg%n"/>
输出:
2013-09-30 12:05:00,167 [main - HelloWorld.java:main:16] com.my.Log4J_test.HelloWorld.main(HelloWorld.java:16) - [example] Hello, World!
2013-09-30 12:05:00,169 [main - HelloWorld.java:main:17] com.my.Log4J_test.HelloWorld.main(HelloWorld.java:17) - [example] Hello, World!
2013-09-30 12:05:00,169 [main - HelloWorld.java:getStackTrace:25] com.my.Log4J_test.HelloWorld.getStackTrace(HelloWorld.java:25) - [example] com.my.Log4J_test.HelloWorld.getStackTrace(HelloWorld.java:23)
2013-09-30 12:05:00,169 [main - HelloWorld.java:getStackTrace:25] com.my.Log4J_test.HelloWorld.getStackTrace(HelloWorld.java:25) - [example] com.my.Log4J_test.HelloWorld.main(HelloWorld.java:19)
2013-09-30 12:05:00,169 [main - HelloWorld.java:getStackTrace:25] com.my.Log4J_test.HelloWorld.getStackTrace(HelloWorld.java:25) - [example] sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
2013-09-30 12:05:00,169 [main - HelloWorld.java:getStackTrace:25] com.my.Log4J_test.HelloWorld.getStackTrace(HelloWorld.java:25) - [example] sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
2013-09-30 12:05:00,169 [main - HelloWorld.java:getStackTrace:25] com.my.Log4J_test.HelloWorld.getStackTrace(HelloWorld.java:25) - [example] sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
2013-09-30 12:05:00,169 [main - HelloWorld.java:getStackTrace:25] com.my.Log4J_test.HelloWorld.getStackTrace(HelloWorld.java:25) - [example] java.lang.reflect.Method.invoke(Method.java:601)
2013-09-30 12:05:00,169 [main - HelloWorld.java:getStackTrace:25] com.my.Log4J_test.HelloWorld.getStackTrace(HelloWorld.java:25) - [example] com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)
因此
%l 输出的location information比 %F %M %L的和还要详细。
其中
(小写) l or location:
Outputs location information of the caller which generated the logging event.
The location information depends on the JVM implementation but usually consists of the fully qualified name of the calling method followed by the callers source the file name and line number between parentheses.
L or line
Outputs the line number from where the logging request was issued.
M or method
Outputs the method name where the logging request was issued.
F or file
Outputs the file name where the logging request was issued.
在其中还有
m msg message 均可表示java code中的event 消息,例如
logger.info("message"); log中就会通过%m显示出字符串message来。
Outputs the application supplied message associated with the logging event.
还有些pattern,如:
r relative 输出程序启动时间。
Outputs the number of milliseconds elapsed since the JVM was started until the creation of the logging event.
最后关于log level的控制是用pattern: p|level{level=label,level=label, ...} p|level{length=n} p|level{lowerCase=true|false 来表示的
Outputs the level of the logging event. You provide a level name map in the form "level=value, level=value" where level is the name of the Level and value is the value that should be displayed instead of the name of the Level.For example:
%level{WARN=Warning, DEBUG=Debug, ERROR=Error, TRACE=Trace, INFO=Info}
Alternatively, for the compact-minded:
%level{WARN=W, DEBUG=D, ERROR=E, TRACE=T, INFO=I}
More succinctly, for the same result as above, you can define the length of the level label:
%level{length=1}
You can combine the two kinds of options:
%level{ERROR=Error, length=2}
Finally, you can output lower-case level names (the default is upper-case):
%level{lowerCase=true}
例如:
<PatternLayout pattern="%d %level{ERROR=hehe} [%t - %F:%M:%L] %l - [example] %msg%n"/>
输出结果就是:
2013-09-30 12:45:55,052 INFO [main - HelloWorld.java:main:16] com.yahoo.Log4J_test.HelloWorld.main(HelloWorld.java:16) - [example] Hello, World!
2013-09-30 12:45:55,054 hehe [main - HelloWorld.java:main:17] com.yahoo.Log4J_test.HelloWorld.main(HelloWorld.java:17) - [example] Hello, World!
瞧,这里重新修改了level的展示名字。
更详细介绍,参见: http://logging.apache.org/log4j/2.x/manual/layouts.html