Uploaded image for project: 'Log4j 2'
  1. Log4j 2
  2. LOG4J2-3556

JsonTemplateLayout truncation ignores exception cause

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Minor
    • Resolution: Fixed
    • None
    • 2.19.0
    • JsonTemplateLayout
    • None

    Description

      Stacktrace truncation is helpful to reduce the size of the event, but current approach implemented in LOG4J2-2993 completely removes any "Caused by:" that might be in the stacktrace, while those are the most useful parts of stacktrace.

      I'm using the modified EcsLayout.json with suggested truncation point

      "error.stack_trace": {
        "$resolver": "exception",
        "field": "stackTrace",
        "stackTrace": {
          "stringified": {
            "truncation": {
              "pointMatcherStrings": ["at javax.servlet.http.HttpServlet.service"],
              "suffix": "\n\t<skipped internal frames>"
            }
          }
        }
      } 

      and the stacktrace I saw on a real production server was rendered as

      org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.NullPointerException
          at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
          at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:909)
          at javax.servlet.http.HttpServlet.service
          <skipped internal frames> 

      which doesn't show anything about origin of the error.

       

      The fix should be relatively simple - change StackTraceStringResolver.truncate method to

          private void truncate(
                  final TruncatingBufferedPrintWriter writer,
                  final int index) {
              int causedByIndex = writer.indexOf("Caused by:");
              CharSequence causedBy = null;
              if (causedByIndex != -1) {
                  causedBy = writer.subSequence(causedByIndex - 1, writer.position());
              }
              writer.position(index);
              writer.print(truncationSuffix);
              if (causedByIndex != -1) {
                  writer.print(causedBy);
              }
          }
       

      and it correctly displays the stack trace:

      java.lang.IllegalArgumentException: wrapper error
      	at reproducer.log4jjsontemplate.StackTraceController.test(StackTraceController.java:18)
      	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      	at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:77)
      	at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
      	at java.base/java.lang.reflect.Method.invoke(Method.java:568)
      	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
      	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:150)
      	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:117)
      	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:895)
      	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:808)
      	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87)
      	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1067)
      	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:963)
      	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
      	at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
      	at javax.servlet.http.HttpServlet.service
      	<skipped internal frames>
      Caused by: java.lang.NullPointerException: original error
      	at reproducer.log4jjsontemplate.StackTraceController.test(StackTraceController.java:16)
      	... 50 more 

       

      I have created an MRE here where you can find more details and monkey patch on a separate branch.

      Attachments

        Issue Links

          Activity

            People

              vy Volkan Yazici
              agavlyukovskiy Arthur Gavlyukovskiy
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: