处理Log4j2不能打印行号的问题(AsyncLogger)

Log4j2不能打印行号问题(AsyncLogger)

背景

%d{yyyy-MM-dd HH:mm:ss,SSS} %level [%t] %logger{36}(%F:%L) - %m%n

日志的输出格式已经指定了文件名、行号显示,可是打印出来却是空的,为什么?答案在这里:

解决方案

pom.xml


    com.lmax
    disruptor
    3.3.11

log4j2.xml


    

注意

  • includeLocation:设置成true,否则指定了行号也显示不出来;
  • disruptor:这个包需要单独引入;
  • additivity:设置成false,否则会重复;

附完整配置节点


    

    
        %date{yyyy-MM-dd HH:mm:ss} %highlight(%-5level) [%boldYellow(%thread)] %boldGreen(%logger)(%F:%L\): %msg%n
    

Log4j2异步日志中打印方法名和行号信息

解决方案

异步logger,还需要在pom.xml中添加disruptor的依赖;

includeLocation结合异步logger使用,当其设置为true时,才会显示具体的行号,以及日志所在的类名;

如果设置为false,哪怕设置了输出行号也不会显示出来;

pom配置

      
            com.lmax
            disruptor
            3.3.6
        

属性配置

设置 AsyncRoot 的 includeLocation 属性为 true;

xml 格式示例:


    

yaml 格式示例:

  Loggers:
      AsyncRoot:
        level: info
        includeLocation: true
        AppenderRef:
          - ref: console
          - ref: running_log

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

你可能感兴趣的:(处理Log4j2不能打印行号的问题(AsyncLogger))