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

Using PatternSelectors breaks header printing in PatternLayout

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • 2.8.2, 2.9.0
    • None
    • Layouts, Pattern Converters
    • None

    Description

      Using a config of

      <?xml version="1.0" ?>
      <Configuration name="LOG4j2-1964 demo" status="ERROR">
        <Appenders>
          <Console name="consoleLog" target="SYSTEM_ERR">
            <PatternLayout pattern="%msg{nolookups}%n%throwable{none}"/>
          </Console>
          <RollingFile name="fooAppender" fileName="foo.log" filePattern="foo.log.%i">
            <PatternLayout header="log file started%n">
              <MarkerPatternSelector defaultPattern="%d{yyyy-MM-dd HH:mm:ss.SSSZ} %msg{nolookups}%n">
                <PatternMatch key="Trace" pattern="%d{yyyy-MM-dd HH:mm:ss.SSSZ} TRACER %msg{nolookups}%n"/>
              </MarkerPatternSelector>
            </PatternLayout>
            <SizeBasedTriggeringPolicy size="10MB"/>
          </RollingFile>
        </Appenders>
        <Loggers>
          <Root level="INFO">
            <AppenderRef ref="consoleLog"/>
            <AppenderRef ref="fooAppender"/>
          </Root>
        </Loggers>
      </Configuration>
      

      the header is expected to be formatted according to the pattern configured but instead the output is

      2017-10-09 14:25:12.072+1300
      2017-10-09 14:25:12.143+1300 using interpolation and a throwable java.lang.NullPointerException
      java.lang.NullPointerException: null
              at leliel.Main.main(Main.java:51) [Log4j2-testing/:?]
              at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[?:1.7.0_79]
              at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57) ~[?:1.7.0_79]
              at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[?:1.7.0_79]
              at java.lang.reflect.Method.invoke(Method.java:606) ~[?:1.7.0_79]
              at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) [idea_rt.jar:?]
      2017-10-09 14:25:12.151+1300 throwable only
      

      The fix appears to simply be to not provide the PatternSelector to the header and footer Serializer builders.

      diff --git a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
      index e4440eb9b..39042081f 100644
      --- a/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
      +++ b/log4j-core/src/main/java/org/apache/logging/log4j/core/layout/PatternLayout.java
      @@ -108,7 +108,7 @@ public final class PatternLayout extends AbstractStringLayout {
                       newSerializerBuilder()
                               .setConfiguration(config)
                               .setReplace(replace)
      -                        .setPatternSelector(patternSelector)
      +                        .setPatternSelector(null)
                               .setAlwaysWriteExceptions(alwaysWriteExceptions)
                               .setDisableAnsi(disableAnsi)
                               .setNoConsoleNoAnsi(noConsoleNoAnsi)
      @@ -117,7 +117,7 @@ public final class PatternLayout extends AbstractStringLayout {
                       newSerializerBuilder()
                               .setConfiguration(config)
                               .setReplace(replace)
      -                        .setPatternSelector(patternSelector)
      +                        .setPatternSelector(null)
                               .setAlwaysWriteExceptions(alwaysWriteExceptions)
                               .setDisableAnsi(disableAnsi)
                               .setNoConsoleNoAnsi(noConsoleNoAnsi)
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            pburrowesOC Paul Burrowes
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated: