用logger 输出printStackTrace()

public class HelloLog4jChild extends HelloLog4j {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
logger.debug("This is debug message!");
logger.info("This is info message!");
logger.error("This is error message!");
HelloLog4jChild object = new HelloLog4jChild();
try {
object.testMethod();
} catch (Exception e) {
// TODO Auto-generated catch block
//e.printStackTrace();
logger.error("HelloLog4jException", e);
}
}

public void testMethod() throws Exception {
try {
testMethod2();
} catch (Exception e) {
// TODO Auto-generated catch block
// e.printStackTrace();
throw new ClassNotFoundException("test1 Exception", e);
}

}

public void testMethod2() throws Exception {

throw new ClassNotFoundException("test2 Exception");
}

}

使用logger.error("ExceptionName",e),可以很轻松的将printStackTrace()输出到log文件中(printStackTrace()是将错误的堆栈信息输出到Console中)。

[quote][11/07/27 13:18:58:812][com.zsk.log4j.demo1.HelloLog4jChild-main] This is info message!
[11/07/27 13:18:58:828][com.zsk.log4j.demo1.HelloLog4jChild-main] This is error message!
[11/07/27 13:18:58:828][com.zsk.log4j.demo1.HelloLog4jChild-main] HelloLog4jException
java.lang.ClassNotFoundException: test1 Exception
at com.zsk.log4j.demo1.HelloLog4jChild.testMethod(HelloLog4jChild.java:29)
at com.zsk.log4j.demo1.HelloLog4jChild.main(HelloLog4jChild.java:15)
Caused by:
java.lang.ClassNotFoundException: test2 Exception
at com.zsk.log4j.demo1.HelloLog4jChild.testMethod2(HelloLog4jChild.java:36)
at com.zsk.log4j.demo1.HelloLog4jChild.testMethod(HelloLog4jChild.java:25)
... 1 more[/quote]
注意Caused by:是不同层级的Exception所产生的
throw new ClassNotFoundException("test1 Exception", e);
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值