假设您执行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之类的记录器,以获取以下信息:
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 );
}