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

Do not prepend whitespace when formatting exception details with ThrowablePatternConverter

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Open
    • Priority: Major
    • Resolution: Unresolved
    • Affects Version/s: 2.11.2
    • Fix Version/s: None
    • Component/s: Layouts, Pattern Converters
    • Labels:
      None

      Description

      The ThrowablePatternConverter prepends whitespace if the character before it is not whitespace. At https://github.com/apache/logging-log4j2/blob/efe06ddfaf7d3b5a2659be6b9f1af4b9a5a3bc95/log4j-core/src/main/java/org/apache/logging/log4j/core/pattern/ThrowablePatternConverter.java#L164-L166 

      len = buffer.length();
      if (len > 0 && !Character.isWhitespace(buffer.charAt(len - 1))) {
        buffer.append(' ');
      }
       

      JsonLayout is not very configurable, outputs a bunch of unneeded garbage for us, and can't modify some of the things it does output (see https://issues.apache.org/jira/browse/LOG4J2-667). It looks like the best option is to use PatternLayout with a custom, escaped JSON message.

      That makes the pattern configuration have pieces:

      "class": "%enc{%throwable{short.className}}{JSON}"
      

      Unfortunately, this means the output comes something like: 

      "class": " java.lang.NumberFormatException"
      

      Which is pretty unfortunate to deal with in filtering and log centralization.

      The workaround (I haven't tried yet) would be to use the replace converter, but it seems quite surprising that a space gets inserted before the converted exception, when none of the other converters seem to do this (except the exception ones)

        Attachments

          Activity

            People

            • Assignee:
              Unassigned
              Reporter:
              mkobit Michael Kobit
            • Votes:
              0 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated: