gpt4 book ai didi

stack-trace - 如何从 logback 自定义布局打印堆栈跟踪?

转载 作者:行者123 更新时间:2023-12-02 18:51:38 25 4
gpt4 key购买 nike

我正在为 logback 编写一个 CustomLayout,因为我想调整线程名称和记录器名称。 logback 文档说

In the above example, the doLayout method ignores any eventual exceptions contained in the event. In a real world layout implementation, you would most probably want to print the contents of exceptions as well.

嗯,是的,我当然想在默认实现时打印堆栈跟踪。但我找不到任何这样做的说明。我下载了源代码并四处查看。以下似乎有效:

/**
* How much stack to print if there's an exception.
*/
private List<String> stackOptionList = Arrays.asList("full");

@Override
public String doLayout(ILoggingEvent event) {
StringBuffer sbuf = new StringBuffer(128);
. . .
IThrowableProxy proxy = event.getThrowableProxy();
if (proxy != null) {
ThrowableProxyConverter converter = new ThrowableProxyConverter();
converter.setOptionList(stackOptionList);
converter.start();
sbuf.append(converter.convert(event));
sbuf.append(CoreConstants.LINE_SEPARATOR);
}
. . .
return sbuf.toString();
}

有更好/更认可的方法吗?

最佳答案

ThrowableProxyConverter 是打印堆栈跟踪的方法。因此,您打算使用的代码看起来不错。但是,您可以使用 custom converters 来调整 PatternLayout,而不是编写 CustomLayout。 。在绝大多数情况下,这是更容易/更好的选择。

关于stack-trace - 如何从 logback 自定义布局打印堆栈跟踪?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16906898/

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