目前大部分项目都是使用slf4j+log4j作为日志框架

Pattern Layouts是一个灵活的布局,是最常用的日志格式配置。本文针对Pattern Layout中的pattern string的常见配置进行梳理

 

1. 简单示例

1.1. 配置

log4j2.xml中的PatternLayout配置:

<PatternLayout pattern="%-d{yyyy-MM-dd HH:mm:ss} %-5r [%t] [%-5p] %c %x - %m%n"/>

<!--或-->

<PatternLayout>
   <Pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %c{3}:%M:%L - %msg%n </Pattern>
</PatternLayout>

1.2. 日志输出

输出为:

2021-05-08 15:43:31 1076  [main] [INFO ] com.suntek.graph.test.basic.SingleExample [] {} - application staring
       2021-05-08 15:43:31 1080  [main] [TRACE] com.suntek.graph.test.basic.SingleExample [] {} - read conf info
       2021-05-08 15:43:31 1082  [main] [INFO ] com.suntek.graph.test.basic.SingleExample [] {ip=172.25.21.22, port=8080} - read conf complete
       2021-05-08 15:43:31 1083  [main] [INFO ] com.suntek.graph.test.basic.SingleExample [ndc log test] {} - start data analyse
       2021-05-08 15:43:31 1083  [main] [INFO ] com.suntek.graph.test.basic.SingleExample [] {} - data analyse complete

2021-05-08 14:52:32.067 [main] INFO  test.basic.SingleExample:main:32 - application staring
       2021-05-08 14:52:32.071 [main] TRACE test.basic.SingleExample:main:33 - read conf info

1.3. 配置解释

数据类型转换字符输出日志
日期

%-d{yyyy-MM-dd HH:mm:ss}

%d{yyyy-MM-dd HH:mm:ss.SSS}

2021-05-08 14:51:59 1048

2021-05-08 14:52:32.071

输出从JVM启动到创建日志事件所经过的毫秒数%-5r1048  
线程名%tmain
记录器名称

%c

%c{3}

com.tech.graph.test.basic.SingleExample

test.basic.SingleExample

日志级别

%-5level 

%-5p

INFO
日志信息

%msg   

%m

read conf info
换行%n日志结束换行
输出所在方法名%Mmain
输出所在行号%L33
输出与生成日志事件的线程相关联的线程上下文堆栈(也称为嵌套诊断上下文或NDC)%x[ndc log test]
输出与生成日志事件的线程相关联的线程上下文映射(也称为映射诊断上下文或MDC)%X{ip=172.25.21.22, port=8080}

1.3. 测试代码

package com.tech.graph.test.basic;

import org.apache.logging.log4j.ThreadContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.MDC;

public class SimpleExample {
    private static final Logger LOG = LoggerFactory.getLogger(SingleExample.class);

    public static void main(String[] args) throws IOException {
        LOG.info("application staring");
        LOG.trace("read conf info");
        MDC.put("ip", "172.25.21.22");
        MDC.put("port", "8080");
        LOG.info("read conf complete");
        MDC.clear();
        ThreadContext.push("ndc log test");
        LOG.info("start data analyse");
        ThreadContext.pop();
        LOG.info("data analyse complete");
    }
}

2. PatternLayout参数

2.1 c{precision}

logger转换说明符后面可以跟着precision精度说明符,后者由一个十进制整数组成,也可以是一个以十进制整数开头的模式。

当精度说明符是一个整数值时,它减少了记录器名称的大小。如果数字为正,则布局将打印最右边的记录器名称组件的相应数量。如果为负,则布局将删除最左侧记录器名称组件的相应数量。

如果精度包含任何非整数字符,则布局将根据模式缩写名称。如果精度整数小于1,布局仍然完整打印最右边的标记。

默认情况下,布局将完整打印日志程序名称。
 

Conversion PatternLogger NameResult
%c{1}org.apache.commons.FooFoo
%c{2}org.apache.commons.Foocommons.Foo
%c{10}org.apache.commons.Fooorg.apache.commons.Foo
%c{-1}org.apache.commons.Fooapache.commons.Foo
%c{-2}org.apache.commons.Foocommons.Foo
%c{-10}org.apache.commons.Fooorg.apache.commons.Foo
%c{1.}org.apache.commons.Fooo.a.c.Foo
%c{1.1.~.~}org.apache.commons.test.Fooo.a.~.~.Foo
%c{.}org.apache.commons.test.Foo....Foo

2.2. %d{pattern}

输出日志事件的日期。日期转换说明符后面可以跟一组大括号,其中包含每个SimpleDateFormat的日期和时间模式字符串。

PatternExample
%d{HH:mm:ss,SSS}14:34:02,123
%d{HH:mm:ss,nnnn} to %d{HH:mm:ss,nnnnnnnnn}14:34:02,1234 to 14:34:02,123456789
%d{dd MMM yyyy HH:mm:ss,SSS}02 Nov 2012 14:34:02,123
%d{dd MMM yyyy HH:mm:ss,nnnn} to %d{dd MMM yyyy HH:mm:ss,nnnnnnnnn}02 Nov 2012 14:34:02,1234 to 02 Nov 2012 14:34:02,123456789
%d{HH:mm:ss}{GMT+0}18:34:02

2.3. 格式对齐

默认情况下,相关信息按原样输出,然而,在格式修饰符的帮助下,可以改变最小字段宽度、最大字段宽度和对齐。

可选的格式修饰符放在百分号和转换字符之间,这是一个十进制常量,表示要输出的最小字符数。

可以是正数,也可以是负数(带减号(-)字符),整数表示右对齐,负数表示左对齐。

如果数据项输出字符数不够,则在左边或右边填充空格,直到达到最小宽度为止。

Format modifierleft justifyminimum widthmaximum widthcomment
%20cfalse20noneLeft pad with spaces if the category name is less than 20 characters long.
%-20ctrue20noneRight pad with spaces if the category name is less than 20 characters long.
%.30cNAnone30Truncate from the beginning if the category name is longer than 30 characters.
%20.30cfalse2030Left 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.
%-20.30ctrue2030Right 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.
%-20.-30ctrue2030Right 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 end.

2.4 L和l属性的区别

l
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.

Generating location information is an expensive operation and may impact performance. Use with caution.

L
line

Outputs the line number from where the logging request was issued.

Generating line number information (location information) is an expensive operation and may impact performance. Use with caution.

配置log4j2.xml:

<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%l] - %msg%n" />
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level [%L] - %msg%n" />

打印出对应的日志:

2019-10-12 15:28:25.877 [main] ERROR [org.apache.logging.log4j.Log4j2Test.logAll(Log4j2Test.java:18)] - error level log
2019-10-12 15:30:34.535 [main] ERROR [18] - error level log

参考

http://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout

http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender

 

Logo

瓜分20万奖金 获得内推名额 丰厚实物奖励 易参与易上手

更多推荐