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

Nested pattern layout options broken

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Resolved
    • Major
    • Resolution: Fixed
    • 2.4.1, 2.9.1
    • 2.10.0
    • Pattern Converters
    • Patch

    Description

      Parsing the "deeply" nested PatternLayout

      %maxLen{[XXX, ${hostName}, ${web:contextPath}] %p: %c{1} - %m%notEmpty{ =>%ex{short}}}{160}
      

      (with %maxLen being a custom Converter)

      results in wrong parsing.
      Patternparser.extractOptions() gets the nesting wrong.

      Solution:

      private static int extractOptions(final String pattern, final int start, final List<String> options) {
          int i = start;
          while (i < pattern.length()  &&  pattern.charAt(i) == '{') {
              i++;                      // skip opening "{"
              final int begin = i;      // position of first real char
              int depth = 1;            // already inside one level
              while (depth > 0  &&  i < pattern.length()) {
                  char c = pattern.charAt(i);
                  if (c == '{') {
                      depth++;
                  } else if (c == '}') {
                      depth--;
                  // TODO(?) maybe escaping of { and } with \ or %
                  }
                  i++;
              } // while
      
              if (depth > 0) {          // option not closed, continue with pattern on opening "{"
                  return begin-1;
              }
      
              options.add(pattern.substring(begin, i-1));
          } // while
      
          return i;
      }
      

      This should also be faster than the current implementation because the pattern ist only walked once, not multiple times with indexOf().

      (LOG4J2-107 is a similar but old bug)

      Attachments

        Activity

          People

            Unassigned Unassigned
            twapache@online.de Thies Wellpott
            Votes:
            1 Vote for this issue
            Watchers:
            8 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: