一寓免、前言
問題:正式服務(wù)器上有一個BUG罩驻,但是本地復(fù)現(xiàn)不了。于是查看日志筝家,發(fā)現(xiàn)日志居然沒有打印有效的錯誤提示轧钓,都是一些代理類的調(diào)用序厉。
代碼:
try{
......
}catch (Exception e) {
e.printStackTrace();
if (BusinessException.class == e.getClass()) {
throw new BusinessException(e.getMessage());
} else {
throw new BusinessException("導(dǎo)入失敗毕箍!");
}
}
日志:
com.xxx.common.exception.BusinessException: 導(dǎo)入失敵诜俊!
at com.xxx.system.service.am.impl.CityMarketServiceImpl.importData(CityMarketServiceImpl.java:964)
at com.xxx.system.service.am.impl.CityMarketServiceImpl$$FastClassBySpringCGLIB$$5ac06249.invoke(<generated>)
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:749)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294)
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.interceptor.ExposeInvocationInterceptor.invoke(ExposeInvocationInterceptor.java:93)
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688)
結(jié)果:拋出的異常被BusinessException覆蓋而柑,導(dǎo)致異常鏈被覆蓋文捶,無法寫入到日志文件中。
二牺堰、解決辦法
private static final Logger log = LoggerFactory.getLogger(CityMarketServiceImpl.class);
try{
......
}catch (Exception e) {
e.printStackTrace();
if (BusinessException.class == e.getClass()) {
throw new BusinessException(e.getMessage());
} else {
log.error(e.getMessage());
throw new BusinessException("導(dǎo)入失斨羟帷!");
}
}
三伟葫、總結(jié)
日志框架使用不夠熟悉恨搓。平時代碼不夠重視日志輸出,沒有仔細研究過這一塊東西筏养。