默认情况下,log4j2在多行上打印stacktrace,并用newline
字符分隔。就像是:
java.lang.NullPointerException: error enovountered
at ...
at ...
at ...
我希望我的stacktrace在单行上,类似,基本上是使用
|
作为分隔符而不是\n
java.lang.NullPointerException: error enovountered at ... | at ... | at ...
如何在log4j2中完成类似的操作?
最佳答案
如 PatternLayout
documentation所指定,转换键的%throwable
系列实际上是support being able to change the separator used for individual stack trace elements, as well as the "cause" exceptions。
给定一个像这样的模式:
[%threadName] %-5level %logger{36} - %message{nolookups}%xThrowable{separator(|)}%n
您将获得如下输出:
[main] ERROR my.cool.Application - Catching java.lang.RuntimeException: I'm wrapping the NPE| at my.cool.Application.main(Application.java:24) [main/:?]|Caused by: java.lang.NullPointerException: This is a forced NPE| at java.util.Objects.requireNonNull(Objects.java:228) ~[?:1.8.0_121]| at my.cool.Application.main(Application.java:21) ~[main/:?]
关于logging - 如何格式化log4j2中的stacktrace?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35789511/