📜  Log4j PatternLayout

📅  最后修改于: 2021-01-06 09:12:48             🧑  作者: Mango

Log4j-PatternLayout

Log4j提供org.apache.log4j.PattrernLayout类,以基于模式以特定格式生成日志记录信息。

PatternLayout扩展了抽象的org.apache.log4j.Layout类,并重写了format()方法以根据提供的模式构造日志记录信息。

PatternLayout也是一个简单的布局对象,提供Bean属性,即conversionPattern ,可以使用配置文件进行设置:

conversionPattern:此属性用于设置转换模式。默认值为%r [%t]%p%c%x-%m%n

模式转换字符

让我们看一下下表,介绍转换模式中使用的字符以及自定义模式中可以使用的所有其他字符:

Conversion Character Meaning
c It is used to output the category of the logging event. For Example: for the category name x.y.z the pattern %c{2} will output y.z.
C It is used to output the fully qualified class name of the caller issuing the logging request. For example, for the class name “org.apache.abc.MyClass”, the pattern %C{1} will output “MyClass”.
d It is 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 It is used to output the file name where the logging request was issued.
l It is used to output location information of the caller which generated the logging event.
L It is used to output the line number from where the logging request was issued.
m It is used to output the application supplied message associated with the logging event.
M It is used to output the method name where the logging request was issued.
n It is used to give the output of platform-dependent line separator character or characters.
p Outputs the priority of the logging event.
r It is used to output the number of milliseconds elapsed from the construction of the layout until the creation of the logging event.
t It is used to output the name of the thread that generated the logging event.
x It is 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 (Mapped Diagnostic Context). For example, X{clientIP} prints the information stored in the MDC against the key clientIP.
% The literal percent sign. %% will print a % sign.

格式修饰符

缺省情况下,相关信息显示为常规输出。但是,log4j提供了格式修饰符。借助于此,可以更改最大字段宽度,最小字段宽度和对齐方式。

让我们看一些修饰符:

Format modifier left justify minimum width maximum width comment
%20c false 20 none Left pad with spaces if the name of the category is less than 20 characters long.
%-20c true 20 none Right pad with spaces if the name of the category is less than 20 characters long.
%.30c NA none 30 Truncate from the beginning if the name of the category is longer than 30 characters.
%20.30c false 20 30 Left pad with spaces if the name of the category is shorter than 20 characters. However, if the name of the category is longer than 30 characters, then truncate from the beginning.
%-20.30c true 20 30 Right pad with spaces if the name of the category is shorter than 20 characters. However, if the category name is longer than 30 characters, then truncate from the beginning.

PatternLayout示例

让我们看一下PatternLayout的一个简单示例。

以下是PatternLayout的简单配置文件:

log4j.properties:

# Define the root logger with appender file
log = /usr/home/log4j
log4j.rootLogger = DEBUG, FILE

# 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

Log4jExample.java:

import org.apache.log4j.Logger;

import java.io.*;
import java.sql.SQLException;
import java.util.*;

public class Log4jExample {
   /* Get actual name of the class 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");
   }
}

编译并运行上述程序时,您将在c:/ usr / home / log4j目录中获得一个log.out文件,该文件包含以下日志信息:

输出:

2019-09-16-main--DEBUG-Log4jExample:Hello this is an debug message
2019-09-16-main--INFO - Log4jExample:Hello this is an info message