在记录器中获取堆栈跟踪

发布于 2025-01-01 15:05:58 字数 997 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

久随 2025-01-08 15:05:58

您必须使用两个参数形式

log.error("my logging message", exception)

请参阅 http://www.devdaily.com/blog/post/java/how-print-exception-stack-trace-using-log4j-commons 了解更多详细信息。

You have to use the two argument form

log.error("my logging message", exception)

See http://www.devdaily.com/blog/post/java/how-print-exception-stack-trace-using-log4j-commons for more details.

空宴 2025-01-08 15:05:58

将您的日志记录语句更改为:

log.error("Exception is: ", e);

Change your logging statement to:

log.error("Exception is: ", e);
︶葆Ⅱㄣ 2025-01-08 15:05:58

实际上是 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.

挽袖吟 2025-01-08 15:05:58

如果您使用以下内容,则将调用 e.toString() ,而 e.toString() 会调用 e.getMessage()

log.error("Exception is:::" + e);

但是,要打印完整的堆栈跟踪,您可以使用以下内容:

log.error("Exception is:::" + ExceptionUtils.getStackTrace(e));

其中 ExceptionUtils 被导入为 org.apache.commons.lang3.exception.ExceptionUtils

If you use the below than e.toString() will be called which calls e.getMessage()

log.error("Exception is:::" + e);

However, to print full stack trace you can use the below:

log.error("Exception is:::" + ExceptionUtils.getStackTrace(e));

where ExceptionUtils is imported as org.apache.commons.lang3.exception.ExceptionUtils

稚然 2025-01-08 15:05:58
log.log(LEVEL.ERROR,e.getMessage(),e);
log.log(LEVEL.ERROR,e.getMessage(),e);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文