gpt4 book ai didi

logging - 如何格式化log4j2中的stacktrace?

转载 作者:行者123 更新时间:2023-12-04 16:33:30 29 4
gpt4 key购买 nike

默认情况下,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/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com