java log日志常见误区
作者:互联网
原因分析
很多程序员会无意间可能会这样打日志
logger.error(e);
logger.error(e.getMessage);
logger.error("错误信息:" + e);
看下logger.error代码就知道error有2个重载方法
public void error(String msg);
public void error(String msg, Throwable t);
上面的代码只有一个参数,因此都会被认为是调用第一种方法,这样造成的结果就是e将会被自动转成String类型,从而丢失的许多错误信息、堆栈信息。
错误示例
如下堆栈等信息基本上全丢失了,即不知道哪个方法调用了service产生的错误,也不知道错误的原因,更不知道代码抛出异常的行数。生产一旦出现问题,根本无从排起,总不能打开电脑debug吧?就像医生问病人哪里不舒服,病人答浑身不舒服,我要是医生当场我就想打人
logger.error("第x部分出错 " + e);
05:10:05.920 [startQuartz_Worker-8] ERROR com.cmx.demo.data.outter.TestService - 第1部分错误
正确示例
而正确的日志,我们即可以知道发生错误的原因,抛出异常的行数,同时也能获悉堆栈调用的关系,这样排查生产问题才有解决的可能性。医生还是问病人哪里不舒服,病人答我因为某某原因,导致左下方腰酸,这下清楚多了
logger.error("第x部分出错", e);
05:10:05.920 [startQuartz_Worker-8] ERROR com.cmx.demo.data.outter.TestService - 当前文件名:DMDXXX.csv 第x部分出错
com.cmx.common.exception.AppException: 第x部分出错
at com.cmx.demo.data.outter.TestService.execAnalyze(TestService.java:169) ~[TestService.class:?]
at com.cmx.demo.data.outter.TestService.execAnalyze(TestService.java:81) [TestService.class:?]
at com.cmx.demo.data.outter.TestService$$FastClassBySpringCGLIB$$4538247f.invoke(<generated>) [TestService.class:?]
at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) [spring-core-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:736) [spring-aop-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:157) [spring-aop-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor$1.proceedWithInvocation(TransactionInterceptor.java:99) [spring-tx-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:283) [spring-tx-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:96) [spring-tx-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:179) [spring-aop-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:671) [spring-aop-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at com.cmx.demo.data.outter.TestService$$EnhancerBySpringCGLIB$$24bd9037.execAnalyze(<generated>) [TestService.class:?]
at com.cmx.demo.data.outter.job.CyberAnalyzeJob.invoke(CyberAnalyzeJob.java:29) [CyberAnalyzeJob.class:?]
at com.cmx.common.job.BaseJob.execJob(BaseJob.java:57) [BaseJob.class:?]
at sun.reflect.GeneratedMethodAccessor142.invoke(Unknown Source) ~[?:?]
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.8.0_60]
at java.lang.reflect.Method.invoke(Method.java:497) ~[?:1.8.0_60]
at org.springframework.util.MethodInvoker.invoke(MethodInvoker.java:265) [spring-core-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.scheduling.quartz.MethodInvokingJobDetailFactoryBean$MethodInvokingJob.executeInternal(MethodInvokingJobDetailFactoryBean.java:257) [spring-context-support-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.springframework.scheduling.quartz.QuartzJobBean.execute(QuartzJobBean.java:75) [spring-context-support-4.3.24.RELEASE.jar:4.3.24.RELEASE]
at org.quartz.core.JobRunShell.run(JobRunShell.java:202) [quartz-2.2.3.jar:?]
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573) [quartz-2.2.3.jar:?]
Caused by: java.io.FileNotFoundException: File D:\xxx\DMDXXX.csv does not exist.
at com.csvreader.CsvReader.<init>(Unknown Source) ~[javacsv.jar:?]
at com.cmx.demo.data.outter.TestService.execAnalyze(TestService.java:157) ~[TestService.class:?]
... 21 more
标签:24,java,log,4.3,jar,TestService,RELEASE,日志 来源: https://blog.csdn.net/chaitoudaren/article/details/104833272