關於log4j日誌列印堆疊的記錄
----遇到的一個日誌的問題,與這位同學遇到的差不多,遂轉載
轉自:
https://blog.csdn.net/weiweiai123456/article/details/72864838
記錄下日誌小細節,逗號和加號在日誌檔案裡的區別
引入jar包:
上程式碼:
這裡實際記錄到日誌檔案裡的日誌資訊是不同的,前者為
逗號在日誌檔案裡體現:
-
2017-06-05 10:33:56 [ERROR] [com.cooya.health.controller.category.CategoryController.updateStatus:159] |【逗號】測試錯誤堆疊到日誌檔案
-
java.lang.NullPointerException: null
-
at com.cooya.health.controller.common.BaseController.getUserName(BaseController.java:48)
-
at com.cooya.health.controller.category.CategoryController.updateStatus(CategoryController.java:155)
-
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
-
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
-
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
-
at java.lang.reflect.Method.invoke(Method.java:601)
-
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
-
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
-
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)
-
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)
-
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)
-
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
-
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)
-
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)
-
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)
-
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)
-
at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)
-
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)
-
at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
-
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)
-
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
-
at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)
-
at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)
-
at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)
-
at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)
-
at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)
-
at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)
-
at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)
-
at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
-
at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
-
at org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:76)
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
-
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)
-
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
-
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)
-
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
-
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)
-
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)
-
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)
-
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
-
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
-
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
-
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
-
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)
-
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
-
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
-
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)
-
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)
-
at java.lang.Thread.run(Thread.java:722)
原因:
void error(String var1, Throwable var2);
第二個引數為Throwable,為Exception的超類。
而如果寫成加號形式,則為:
2017-06-05 10:33:56 [ERROR] [com.cooya.health.controller.category.CategoryController.updateStatus:160] |【加號】這裡是測試錯誤資訊列表java.lang.NullPointerException
這是因為:
void error(String var1);
相當於拼接字串了,所以加號和逗號對於日誌儲存差異很大。
mark一個知識,也是基礎細節。Exception e
e.getMessage();
e.toString();的區別,一張圖表達。
總結:細節決定程式碼差距,以前這裡都沒有注意,感覺逗號和加號差不多,在生產中調取日誌檔案才發現沒有堆疊資訊。