1. 程式人生 > >關於log4j日誌列印堆疊的記錄

關於log4j日誌列印堆疊的記錄

----遇到的一個日誌的問題,與這位同學遇到的差不多,遂轉載

轉自:

https://blog.csdn.net/weiweiai123456/article/details/72864838

記錄下日誌小細節,逗號和加號在日誌檔案裡的區別

 

引入jar包:

 

上程式碼:

 

這裡實際記錄到日誌檔案裡的日誌資訊是不同的,前者為

逗號在日誌檔案裡體現:

 

 
  1. 2017-06-05 10:33:56 [ERROR] [com.cooya.health.controller.category.CategoryController.updateStatus:159] |【逗號】測試錯誤堆疊到日誌檔案

  2. java.lang.NullPointerException: null

  3. at com.cooya.health.controller.common.BaseController.getUserName(BaseController.java:48)

  4. at com.cooya.health.controller.category.CategoryController.updateStatus(CategoryController.java:155)

  5. at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)

  6. at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)

  7. at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)

  8. at java.lang.reflect.Method.invoke(Method.java:601)

  9. at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)

  10. at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)

  11. at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:110)

  12. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:832)

  13. at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:743)

  14. at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)

  15. at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:961)

  16. at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:895)

  17. at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:967)

  18. at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:869)

  19. at javax.servlet.http.HttpServlet.service(HttpServlet.java:641)

  20. at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:843)

  21. at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

  22. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)

  23. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

  24. at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:77)

  25. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  26. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

  27. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

  28. at org.apache.shiro.web.servlet.AbstractShiroFilter.executeChain(AbstractShiroFilter.java:449)

  29. at org.apache.shiro.web.servlet.AbstractShiroFilter$1.call(AbstractShiroFilter.java:365)

  30. at org.apache.shiro.subject.support.SubjectCallable.doCall(SubjectCallable.java:90)

  31. at org.apache.shiro.subject.support.SubjectCallable.call(SubjectCallable.java:83)

  32. at org.apache.shiro.subject.support.DelegatingSubject.execute(DelegatingSubject.java:383)

  33. at org.apache.shiro.web.servlet.AbstractShiroFilter.doFilterInternal(AbstractShiroFilter.java:362)

  34. at org.apache.shiro.web.servlet.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:125)

  35. at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)

  36. at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)

  37. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

  38. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

  39. at org.jasig.cas.client.session.SingleSignOutFilter.doFilter(SingleSignOutFilter.java:76)

  40. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

  41. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

  42. at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:121)

  43. at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)

  44. at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:243)

  45. at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)

  46. at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:224)

  47. at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:185)

  48. at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:151)

  49. at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)

  50. at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)

  51. at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)

  52. at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)

  53. at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:269)

  54. at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)

  55. at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)

  56. at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1110)

  57. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:603)

  58. 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();的區別,一張圖表達。

 

總結:細節決定程式碼差距,以前這裡都沒有注意,感覺逗號和加號差不多,在生產中調取日誌檔案才發現沒有堆疊資訊。