问题描述:
最近项目想打印更详细的日志信息,包括打印日志的位置和方法。在log4j2的配置文件中为格式添加%L和%M后,发现并不能打印出行号和方法名。
不了解log4j2配置文件请参考log4j2配置文件解析。
问题分析:
一通乱七八糟的操作后,毫无用处,还是乖乖的去看官方文档吧。由于配置文件使用的是PatternLayout,所以在官网找到PatternLayout,找到对L属性的描述:
貌似还有更详细的介绍。点蓝色的链接(官网链接:LocationInformation),可以看到在Location Information中有如下描述:
If one of the layouts is configured with a location-related attribute like HTML locationInfo, or one of the patterns %C or %class, %F or %file, %l or %location, %L or %line, %M or %method, Log4j will take a snapshot of the stack, and walk the stack trace to find the locat