http://www.yiibai.com/log4j/log4j_patternlayout.html
描述
log4j PatternLayout实例教程 - 如果想在一个特定的格式基础上一个模式产生的日志信息,那么可以使用org.apache.log4j.PatternLayout来格式化您的日志信息。
如果想在一个特定的格式基础上一个模式产生的日志信息,那么可以使用org.apache.log4j.PatternLayout来格式化日志信息。
PatternLayout类扩展了抽象org.apache.log4j.Layout类,并覆盖format()方法来结构日志信息根据提供的模式。
PatternLayout是一个简单的布局对象提供了以下Bean属性可以设置使用配置文件:
| S.N. | Property & Description |
|---|---|
| 1 | conversionPattern 设置转换模式。默认值是 %r [%t] %p %c %x - %m%n |
模式转换字符:
下面的表格解释在上述模式中使用的字符和所有其他字符,可以使用你自定义的模式:
| Conversion Character | Meaning |
|---|---|
| c | Used to output the category of the logging event. For example, for the category name "a.b.c" the pattern %c{2} will output "b.c". |
| C | Used to output the fully qualified class name of the caller issuing the logging request. For example, for the class name "org.apache.xyz.SomeClass", the pattern %C{1} will output "SomeClass". |
| d | Used to output the date of the logging event. For example, %d{HH:mm:ss,SSS} or %d{dd MMM yyyy HH:mm:ss,SSS}. |
| F | Used to output the file name where the logging request was issued. |
| l | Used to output location information of the caller which generated the logging event. |
| L | Used to output the line number from where the logging request was issued. |
| m | Used to output the application supplied message associated with the logging event. |
| M | Used to output the method name where the logging request was issued. |
| n | Outputs the platform dependent line separator character or characters. |
| p | Used to output the priority of the logging event. |
| r | Used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event. |
| t | Used to output the name of the thread that generated the logging event. |
| x | Used to output the NDC (nested diagnostic context) associated with the thread that generated the logging event. |
| X | The X conversion character is followed by the key for the MDC. For example, X{clientIP} will print the information stored in the MDC against the key clientIP. |
| % | The literal percent sign. %% will print a % sign. |
格式修饰符:
默认情况下,相关的信息输出。在格式修饰符的帮助下,它是可以改变的最小字段宽度,最大字段宽度和对齐。
以下表涵盖了各种修饰符的情况:
| Format modifier | left justify | minimum width | maximum width | comment |
|---|---|---|---|---|
| %20c | false | 20 | none | Left pad with spaces if the category name is less than 20 characters long. |
| %-20c | true | 20 | none | Right pad with spaces if the category name is less than 20 characters long. |
| %.30c | NA | none | 30 | Truncate from the beginning if the category name is longer than 30 characters. |
| %20.30c | false | 20 | 30 | Left pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, hen truncate from the beginning. |
| %-20.30c | true | 20 | 30 | Right pad with spaces if the category name is shorter than 20 characters. However, if category name is longer than 30 characters, then truncate from the beginning. |
PatternLayout 例子:
以下是一个简单的配置文件PatternLayout:
# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = DEBUG, FILE
/* http://www.yiibai.com/log4j */
# Define the file appender
log4j.appender.FILE=org.apache.log4j.FileAppender
log4j.appender.FILE.File=${log}/log.out
# Define the layout for file appender
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout
log4j.appender.FILE.layout.ConversionPattern=
%d{yyyy-MM-dd}-%t-%x-%-5p-%-10c:%m%n
现在考虑下面的的Java示例将产生的日志信息:
import org.apache.log4j.Logger;
import java.io.*;
import java.sql.SQLException;
import java.util.*;
public class log4jExample{
/* Get actual class name to be printed on */
static Logger log = Logger.getLogger(
log4jExample.class.getName());
public static void main(String[] args)
throws IOException,SQLException{
log.debug("Hello this is an debug message");
log.info("Hello this is an info message");
}
}
编译并运行上述程序,将创建一个log.out文件在/usr/home/log4j目录,将看日志信息:
2010-03-23-main--DEBUG-log4jExample:Hello this is an debug message 2010-03-23-main--INFO -log4jExample:Hello this is an info message