最佳答案
假设您在执行 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)
现在我想把它发送给一个 logger,比如 log4j,得到以下结果:
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 );
}