本文介绍了log4j没有打印异常的stacktrace的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用log4j和tomcat。当我在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.

根本不是很有帮助!

我的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:

来自:

服务器VM中的编译器现在为所有冷内置异常提供正确的堆栈回溯
。出于性能目的,当
抛出这样的异常几次时,可以重新编译该方法。
重新编译之后,编译器可以使用
预分配的异常选择更快的策略,这些异常不提供堆栈跟踪。要完全禁用
使用预分配的异常,请使用以下新标志:
-XX:-OmitStackTraceInFastThrow。

更多信息:

这篇关于log4j没有打印异常的stacktrace的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 12:44