Java Log4j 格式:是否可以截断堆栈跟踪?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3263741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 19:07:33  来源:igfitidea点击:

Log4j formatting: Is it possible to truncate stacktraces?

javalogginglog4jstack-trace

提问by rompetroll

I want to log only the first few lines of Exceptions in my program. I know, I can do something like this to print only the first 5 lines of a stacktrace:

我只想在我的程序中记录异常的前几行。我知道,我可以做这样的事情来只打印堆栈跟踪的前 5 行:

Throwable e = ...;
StackTraceElement[] stack = e.getStackTrace();
int maxLines = (stack.length > 4) ? 5 : stack.length;
for (int n = 0; n < maxLines; n++) {
    System.err.println(stack[n].toString());
}

But I would rather use log4j (or slf4j over log4j to be more precise) for logging. Is there a way to tell log4j that it should only print the first 5 lines of a stacktrace?

但我宁愿使用 log4j(或 slf4j 而不是 log4j 更精确)进行日志记录。有没有办法告诉 log4j 它应该只打印堆栈跟踪的前 5 行?

采纳答案by Hendrik

You can use a EnhancedPatternLayout in log4j to format your stacktraces.

您可以在 log4j 中使用 EnhancedPatternLayout 来格式化堆栈跟踪。

See http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/EnhancedPatternLayout.html, specifically the section about the "throwable" pattern in the pattern table.

参见http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/EnhancedPatternLayout.html,特别是关于模式表中“throwable”模式的部分。

Note that support for the %throwable{n}support is rather new and requires at least log4j 1.2.16 (which is the latest at time of writing)

请注意,对支持的%throwable{n}支持是相当新的,并且至少需要 log4j 1.2.16(这是撰写本文时的最新版本)

For tracking purposes, this is the ticket that dealt with its implementation: https://issues.apache.org/bugzilla/show_bug.cgi?id=48902

出于跟踪目的,这是处理其实施的票证:https: //issues.apache.org/bugzilla/show_bug.cgi?id=48902

回答by Bozho

I don't know of such an option. But you can extend your current appender (for example RollingFileAppender) and provide an append/ doAppend/ subAppendmethod (depending on which class you are extending) to handle this.

我不知道有这样的选择。但是,您可以扩展您当前的appender(例如RollingFileAppender),并提供append/ doAppend/subAppend方法(根据您所延伸的类)来处理这个问题。

The throwable information is contained in LoggingEvent.throwableInformation

可抛出的信息包含在 LoggingEvent.throwableInformation

That said, I'm not sure you should do this - you can lose important information that way.

也就是说,我不确定您是否应该这样做 - 这样可能会丢失重要信息。

回答by prav

Yup... EnhancedPatternLayout provides this functionality. (Since Log4J-1.2.16, was in extra companions before).

是的... EnhancedPatternLayout 提供了这个功能。(从 Log4J-1.2.16 开始,之前是在额外的同伴中)。

For a log4j config of

对于 log4j 配置

<appender name="Console" class="org.apache.log4j.ConsoleAppender">
    <param name="Threshold" value="debug"/>
    <layout class="org.apache.log4j.EnhancedPatternLayout">
        <param name="ConversionPattern" value="%d %-5p [%t] %c.%M - %m%n %throwable{short}"/>
    </layout>
</appender>

and for a piece of Java code like

对于一段 Java 代码,如

throw new Exception(new Exception("Inner Exception"));

throw new Exception(new Exception("内部异常"));

You get the following in the log file...

您在日志文件中得到以下内容...

java.lang.Exception: java.lang.Exception: Inner Exception

java.lang.Exception: java.lang.Exception: 内部异常

If we remove the '%throwable{short}' from our log4j config file we would get the full stack trace

如果我们从 log4j 配置文件中删除 '%throwable{short}',我们将获得完整的堆栈跟踪

回答by Kai Sternad

Yes, it is part of log4j since log4j 1.2.16

是的,它是 log4j 的一部分,因为 log4j 1.2.16

Here is the original proposal in the Apache issue tracker that describes in detail how each of the applications of the %throwable pattern looks like: https://issues.apache.org/bugzilla/show_bug.cgi?id=48902#c0

这是 Apache 问题跟踪器中的原始提案,详细描述了 %throwable 模式的每个应用程序的外观:https://issues.apache.org/bugzilla/show_bug.cgi?id =48902#c0

回答by vsingh

In log4j2.xml simply add %throwable{short} at end. No need to add param name.

在 log4j2.xml 中,只需在末尾添加 %throwable{short}。无需添加参数名称。

<PatternLayout pattern="%d{yyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %m%n %throwable{short} %n"/>

ref : https://logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout

参考:https: //logging.apache.org/log4j/2.x/manual/layouts.html#PatternLayout