本文介绍了如何将堆栈跟踪发送到 log4j?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
假设你捕获一个异常并在标准输出(比如控制台)上得到以下内容,如果你执行 e.printStackTrace() :
Say you catch an exception and get the following on the standard output (like, say, the console) if you do a e.printStackTrace() :
java.io.FileNotFoundException: so.txt
at java.io.FileInputStream.<init>(FileInputStream.java)
at ExTest.readMyFile(ExTest.java:19)
at ExTest.main(ExTest.java:7)
现在我想把它发送给一个记录器,比如 log4j 以获得以下内容:
Now I want to send this instead to a logger like, say, log4j to get the following:
31947 [AWT-EventQueue-0] ERROR Java.io.FileNotFoundException: so.txt
32204 [AWT-EventQueue-0] ERROR at java.io.FileInputStream.<init>(FileInputStream.java)
32235 [AWT-EventQueue-0] ERROR at ExTest.readMyFile(ExTest.java:19)
32370 [AWT-EventQueue-0] ERROR at ExTest.main(ExTest.java:7)
我该怎么做?
try {
...
} catch (Exception e) {
final String s;
... // <-- What goes here?
log.error( s );
}
推荐答案
您将异常直接传递给记录器,例如
You pass the exception directly to the logger, e.g.
try {
...
} catch (Exception e) {
log.error( "failed!", e );
}
由 log4j 来呈现堆栈跟踪.
It's up to log4j to render the stack trace.
这篇关于如何将堆栈跟踪发送到 log4j?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!