在记录器中获取堆栈跟踪
我正在使用 log4j 来记录我的异常。我想记录在 e.printStackTrace();
中得到的所有内容 我的代码如下所示:
try {
} catch(Exception e) {
log.error("Exception is:::" + e);
}
但我记录的内容如下所示:
2012-02-02 12:47:03,227 ERROR [com.api.bg.sample] - Exception in unTech:::[Ljava.lang.StackTraceElement;@6ed322
2012-02-02 12:47:03,309 ERROR [com.api.bg.sample] - Exception is :::java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
但我期望的内容是:
java.io.IOException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at com.api.bg.sample.unGZIP(sample.java:191)
at com.api.bg.sample.main(sample.java:69)
我尝试了 e.getMessage()
, e.getStackTrace();
但是我没有得到完整的堆栈跟踪。有什么建议吗?
I am using log4j to log my exceptions. I want to log whatever I get in e.printStackTrace();
My code looks like this:
try {
} catch(Exception e) {
log.error("Exception is:::" + e);
}
But the content I get logged looks like this:
2012-02-02 12:47:03,227 ERROR [com.api.bg.sample] - Exception in unTech:::[Ljava.lang.StackTraceElement;@6ed322
2012-02-02 12:47:03,309 ERROR [com.api.bg.sample] - Exception is :::java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
But the content I expect is:
java.io.IOException: Not in GZIP format
at java.util.zip.GZIPInputStream.readHeader(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at java.util.zip.GZIPInputStream.<init>(Unknown Source)
at com.api.bg.sample.unGZIP(sample.java:191)
at com.api.bg.sample.main(sample.java:69)
I tried e.getMessage()
, e.getStackTrace();
however I don't get the full stacktrace. Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
您必须使用两个参数形式
请参阅 http://www.devdaily.com/blog/post/java/how-print-exception-stack-trace-using-log4j-commons 了解更多详细信息。
You have to use the two argument form
See http://www.devdaily.com/blog/post/java/how-print-exception-stack-trace-using-log4j-commons for more details.
将您的日志记录语句更改为:
Change your logging statement to:
实际上是 log4j 阻止了全时堆栈跟踪的打印。但是,您应该将异常设置为错误方法的第二个参数。
It is actualy log4j that prevents the printing of the fulltime stacktrace. You should however set the exception as a second parameter for the error method.
如果您使用以下内容,则将调用 e.toString() ,而 e.toString() 会调用 e.getMessage()
但是,要打印完整的堆栈跟踪,您可以使用以下内容:
其中 ExceptionUtils 被导入为 org.apache.commons.lang3.exception.ExceptionUtils
If you use the below than e.toString() will be called which calls e.getMessage()
However, to print full stack trace you can use the below:
where ExceptionUtils is imported as org.apache.commons.lang3.exception.ExceptionUtils