log4j 不打印异常的堆栈跟踪 [英] log4j not printing the stacktrace for exceptions
问题描述
我在 tomcat 中使用 log4j.当我在 JSP、servlet 中记录异常时:
I am using log4j with tomcat. When I log exceptions in my JSPs, servlets:
private Logger _log = Logger.getLogger(this.getClass());
...
try{...} catch (Exception e) {
_log.error("Error refreshing all prices", e);
}
我只得到异常的第一行,没有堆栈跟踪.
I only get the first line of the exception, without a stacktrace.
2 月 17 日 17:37:45 错误 AutoContrib:175 - 发布 csv 文件时出现异常:java.lang.ArrayIndexOutOfBoundsException
17-Feb 17:37:45 ERROR AutoContrib:175 - Exception while publishing csv file: java.lang.ArrayIndexOutOfBoundsException
一点帮助都没有!
我的 log4j.properties 文件 (/tomcat/common/classes/log4j.properties) 如下所示:
My log4j.properties file (/tomcat/common/classes/log4j.properties) looks like this:
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd-MMM HH:mm:ss} %5p %c{1}:%L - %m%n
log4j.appender.stdout.threshold=info
log4j.appender.file=org.apache.log4j.RollingFileAppender
log4j.appender.file.maxFileSize=5000KB
log4j.appender.file.maxBackupIndex=10
log4j.appender.file.File=${catalina.home}/logs/web.log
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{dd-MMM HH:mm:ss} %5p %c{1}:%L - %m%n
log4j.appender.file.threshold=info
log4j.rootLogger=debug, stdout, file
推荐答案
实际上,这可能是由于热点优化:在抛出一定数量的相同异常后,它停止打印跟踪.这可以通过 VM arg 关闭,请参阅:
Actually, it's probably due to a hotspot optimization: after a certain number of the same exception being thrown it stops printing out trace. This can be turned off with a VM arg, see:
来自 http://www.oracle.com/technetwork/java/javase/relnotes-139183.html :
服务器 VM 中的编译器现在提供正确的堆栈回溯对于所有冷"内置异常.出于性能目的,当这样的异常抛出几次,方法可能会被重新编译.重新编译后,编译器可能会选择使用更快的策略不提供堆栈跟踪的预分配异常.禁用完全使用预分配的异常,使用这个新标志:-XX:-OmitStackTraceInFastThrow.
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 不打印异常的堆栈跟踪的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!