本文介绍了如何在log4j2中格式化堆栈跟踪?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

默认情况下,log4j2以newline字符分隔的多行打印堆栈跟踪。类似于:

java.lang.NullPointerException: error enovountered
    at ...
    at ...
    at ...

我希望我的堆栈跟踪在一行上,例如,基本上使用|作为分隔符,而不是

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/:?]

这篇关于如何在log4j2中格式化堆栈跟踪?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-14 18:09