log4j2之Layouts学习笔记

来源:互联网 发布:连云港联通网络电视 编辑:程序博客网 时间:2024/06/05 14:56

首先Layout是布局的意思。在Log4j2中Layouts用来表示日志输入的形式和样式。
Layouts的输出形式多种多样包括CVS Layout,HTML Layout,JSON Layout,Pattern Layout等。
本次我学习了其中的Pattern Layout。
原文:
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 the conversion pattern.
(大意是说,这是一种可通过模式化的字符串配置的布局,用来记录一个日志事件,输出的样式取决于转换模式)。
The conversion pattern is closely related to the converion pattern of the printf function in C.A conversion pattern is composed of literal text and format control expresssions called voncersion specifiers.
(大意是说,这种转换模式和printf函数中的转换模式相似,这种转换模式由普通字符和格式控制表达式组成,这种转换模式成为转换说明符。)
Each conversion specifies starts with a percent sign(%) and is followed by optional format 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");

would yield the output

DEBUG [main]: Message 1WARN  [main]: Message 2

Note that there is no explicit separator between text and 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.
(大意是指,这是一个例子,假设在log4j的环境下已经设置好使用PatternLayout。注意:在文本和转换说明符之间并没有特定的分隔符,%-5p输出的内容不足5个字符左对齐右边留空白,p表示输出的是日志的级别。

总结
通过学习本节,我知道了conversion specifier(格式说明符)是由%打头,后面接一个format modifier(格式修饰符:输出内容的宽度,左右对齐等)和一个conversion character(转换符:用来表示特定含义的内容,比如日期,线程名称,目录等)

原创粉丝点击