了解Log4J2异常(抛出)输出



我试图弄清楚Log4J2输出的throwing字段的输出。

此代码故意生成一个异常,并将该异常发送到Log4J2:

class HelloWorld1 {
private static final Logger logger = LogManager.getLogger(HelloWorld1.class.getName());
public void method() {
try {
System.out.println("Division: " + (1 / 0));
} catch (Exception ex) {
logger.error("Got exception", ex);
}
}

检查日志输出,我得到这样的结果:

{
...
"level": "ERROR",
"loggerName": "logforj2.HelloWorld1",
"message": "Got exception",
"thrown": {
"commonElementCount": 0,
"localizedMessage": "/ by zero",
"message": "/ by zero",
"name": "java.lang.ArithmeticException",
"extendedStackTrace": [
{
"class": "logforj2.HelloWorld1",
"method": "main",
"file": "HelloWorld1.java",
"line": 23,
"exact": true,
"location": "classes/",
"version": "?"
}
]
},
...
}

到目前为止还不错,但我在网上看到的例子显示了异常的不同输出,这些输出的字段比我预期的要多得多:

{
...
"level": "DEBUG",
"message": "Msg",
"thrown": {
"commonElementCount": 0,
"localizedMessage": "testIOEx",
"message": "testIOEx",
"name": "java.io.IOException",
"extendedStackTrace": [
{
"class": "org.apache.logging.log4j.core.layout.LogEventFixtures",
"method": "createLogEvent",
"file": "LogEventFixtures.java",
"line": 56,
"exact": true,
"location": "test-classes/",
"version": "?"
},
{
"class": "org.apache.logging.log4j.core.layout.JsonLayoutTest",
"method": "testAllFeatures",
"file": "JsonLayoutTest.java",
"line": 105,
"exact": true,
"location": "test-classes/",
"version": "?"
}...
],
"cause": {
"commonElementCount": 27,
"extendedStackTrace": [
{
"class": "org.apache.logging.log4j.core.layout.LogEventFixtures",
"method": "createLogEvent",
"file": "LogEventFixtures.java",
"line": 53,
"exact": false,
"location": "test-classes/",
"version": "?"
}
],
"localizedMessage": "testNPEx",
"message": "testNPEx",
"name": "java.lang.NullPointerException"
},
"suppressed": [
{
"commonElementCount": 0,
"localizedMessage": "I am suppressed exception 1",
"message": "I am suppressed exception 1",
"name": "java.lang.IndexOutOfBoundsException",
"extendedStackTrace": [
{
"class": "org.apache.logging.log4j.core.layout.LogEventFixtures",
"method": "createLogEvent",
"file": "LogEventFixtures.java",
"line": 57,
"exact": true,
"location": "test-classes/",
"version": "?"
},
{
"class": "org.apache.logging.log4j.core.layout.JsonLayoutTest",
"method": "testAllFeatures",
"file": "JsonLayoutTest.java",
"line": 105,
"exact": true,
"location": "test-classes/",
"version": "?"
}...
]
},
{
"commonElementCount": 0,
"localizedMessage": "I am suppressed exception 2",
"message": "I am suppressed exception 2",
"name": "java.lang.IndexOutOfBoundsException",
"extendedStackTrace": [
{
"class": "org.apache.logging.log4j.core.layout.LogEventFixtures",
"method": "createLogEvent",
"file": "LogEventFixtures.java",
"line": 58,
"exact": true,
"location": "test-classes/",
"version": "?"
}...
],

}
]
},
...
}

我的问题是,我不了解大部分领域。例如:

  1. 为什么它被称为extendedStackTrace?是否有其他未扩展的堆栈跟踪?有什么区别
  2. commonElementCount是什么意思
  3. localizedMessagemessage之间有什么区别
  4. cause字段是什么意思
  5. suppressed字段是什么意思
  1. 您在输出中看到的内容取决于您选择的布局和配置方式。PatternLayout描述了3种不同类型的可抛出PatternConverter;默认的ThrowablePatternConverter、ExtendedSthrowablePatternConvert和RootThrowablePatternConverter

它们之间的区别是:

  • 默认的ThrowablePatternConverter打印堆栈跟踪,就像您在Throwable.printStackTrace((中看到的一样(事实上,这就是生成输出的方式(
  • ExtendedSthrowablePatternConverter包括堆栈跟踪中每一行的jar的名称和版本。这在诊断问题时非常有用
  • RootThrowablePatternConverter打印最里面的";由";exception首先跟在链中的每个Throwables后面。换言之,它以ThrowablePatternConverter的相反顺序打印Exceptions,以便首先打印出现故障的异常,这可以节省一些时间

从上面的示例来看,您似乎正在使用JsonLayout。该Layout使用Jackson对其输出进行格式化,并指定LogEvent包括ExtendedStakTrace,其中将包括jar和版本信息。

  1. 几乎可以肯定的是,当异常被链接时,每个异常都将具有与其链接的异常相同的堆栈帧。一旦找到了第一个公共元素,堆栈跟踪底部的所有其他元素也几乎总是公共的。该字段标识有多少个元素与允许不打印公共元素的前置元素相同
  2. localizedMessage和message记录在java.lang.Throwable中
  3. 原因是链条上的下一个投掷物。java.lang.Throwable中也对其进行了说明
  4. 抑制是一组被抑制的异常。再次参见java.lang.Throwable

最新更新