You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@logging.apache.org by "Arthur Gavlyukovskiy (Jira)" <ji...@apache.org> on 2022/07/19 17:20:00 UTC

[jira] [Created] (LOG4J2-3556) JsonTemplateLayout truncation ignores exception cause

Arthur Gavlyukovskiy created LOG4J2-3556:
--------------------------------------------

             Summary: JsonTemplateLayout truncation ignores exception cause
                 Key: LOG4J2-3556
                 URL: https://issues.apache.org/jira/browse/LOG4J2-3556
             Project: Log4j 2
          Issue Type: Bug
          Components: JsonTemplateLayout
            Reporter: Arthur Gavlyukovskiy


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
{code:java}
"error.stack_trace": {
  "$resolver": "exception",
  "field": "stackTrace",
  "stackTrace": {
    "stringified": {
      "truncation": {
        "pointMatcherStrings": ["at javax.servlet.http.HttpServlet.service"],
        "suffix": "\n\t<skipped internal frames>"
      }
    }
  }
} {code}
and the stacktrace I saw on a real production server was rendered as
{code:java}
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> {code}
which doesn't show anything about origin of the error.

 

The fix should be relatively simple - change StackTraceStringResolver.truncate method to
{code:java}
    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);
        }
    }
 {code}
and it correctly displays the stack trace.

 

I have created an MRE [here|https://github.com/gavlyukovskiy/log4j-json-template-reproducer] where you can find more details and monkey patch on a [separate branch|https://github.com/gavlyukovskiy/log4j-json-template-reproducer/tree/monkey-patch].



--
This message was sent by Atlassian Jira
(v8.20.10#820010)