Logger 仅打印日志消息而不打印异常

Logger prints only the log message and not the exception

try {
   // some code
}
catch (Exception e) {
   Logger.log(Level.WARN, "Unable to complete the job. ID: " + id, e);
}

所以,开发人员显然希望如果出现问题,它会记录异常(异常类型和堆栈跟踪)

这是我得到的打印日志

[27 May 2019 13:30:07][http-nio-8080-exec-13][WARN]: Unable to complete the job. ID: 123457890

这是 Log4j 配置

log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.File=debug.log
log4j.appender.file.MaxFileSize=1MB
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=[%d{dd MMM yyyy HH:mm:ss}][%t][%p]: %m%n

我知道获取异常详细信息的方法,我想了解此行为。这是怎么发生的?记录器是否忽略传递的参数?

我唯一的猜测是,这是由于 热点优化:

The compiler in the server VM now provides correct stack backtraces for all "cold" built-in exceptions. For performance purposes, when such an exception is thrown a few times, the method may be recompiled. After recompilation, the compiler may choose a faster tactic using preallocated exceptions that do not provide a stack trace. To disable completely the use of preallocated exceptions, use this new flag: -XX:-OmitStackTraceInFastThrow.

这是从对相关问题的回答中借用的:

log4j not printing the stacktrace for exceptions

ERROR 级别用于在日志文件中记录错误的堆栈跟踪。

尝试使用。

    try {
        // some code
    } catch (Exception e) {
       Logger.log(Level.ERROR, "Unable to complete the job. ID: " + id, e);
    }

如果还是不行,请分享你的日志库。将检查那个。