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

2.17.1: JsonLayout Context no longer working.

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.17.1
    • 2.17.2
    • Layouts
    • None

    Description

      We're creating a RollingFileAppender programmatically using a JsonLayout. This uses fields using ${ctx:...} expansion. This stopped working when upgrading from 2.17.0 to 2.17.1.

      The code is as follows:

      /**
           * Creates a rolling file appender that write audit entries to a programmatically readable JSON file.
           */
          private RollingFileAppender createJsonAppender(Path logDir) {
              List<KeyValuePair> fields = new ArrayList<>();
              fields.add(KeyValuePair.newBuilder().setKey("who").setValue("${ctx:WHO}").build());
              fields.add(KeyValuePair.newBuilder().setKey("what").setValue("${ctx:WHAT}").build());
              fields.add(KeyValuePair.newBuilder().setKey("method").setValue("${ctx:METHOD}").build());
              fields.add(KeyValuePair.newBuilder().setKey("parameters").setValue("${ctx:PARAMETERS}").build());        
              RollingFileAppender.Builder<?> builder = RollingFileAppender.newBuilder();
              builder.setName("auditJsonLogger");
              builder.withFileName(logDir.resolve(LOG_JSON_FILENAME).toString());
              builder.withFilePattern(logDir.resolve(LOG_JSON_FILEPATTERN).toString());
              builder.withPolicy(SizeBasedTriggeringPolicy.createPolicy("10M"));
              builder.setLayout(JsonLayout.newBuilder().setCompact(true).setEventEol(true).setConfiguration(new DefaultConfiguration())
                      .setAdditionalFields(fields.toArray(new KeyValuePair[0])).build());
              builder.withStrategy(
                      DefaultRolloverStrategy.newBuilder().withCompressionLevelStr(String.valueOf(Deflater.DEFAULT_COMPRESSION))
                              .withMax(Integer.toString(LOG_MAX_INDEX)).withFileIndex("min").build());
              return builder.build();
          } 

      We also tried to use %X{...} as well as $${ctx:...} expansions instead to no avail. The log file always contains the literal string we use in .setValue(), i.e. lines like this:

      {"instant":{"epochSecond":1642756242,"nanoOfSecond":271091900},"thread":"hive/fsck","level":"INFO","message":"Execute","endOfBatch":false,"threadId":65,"threadPriority":5,"who":"${ctx:WHO}","what":"${ctx:WHAT}","method":"${ctx:METHOD}","parameters":"${ctx:PARAMETERS}"}

      Reverting to 2.17.0 will correctly give lines like this in the JSON file:

      {"instant":{"epochSecond":1642756430,"nanoOfSecond":923210700},"thread":"hive/fsck","level":"INFO","message":"Execute","endOfBatch":false,"threadId":62,"threadPriority":5,"who":"marku","what":"ObjectConsistencyCheckOperation","method":"-","parameters":"{dryRun=false, roots=[io.bdeploy/demo/client-app/linux:1.0.0, io.bdeploy/demo/client-app/linux:2.0.0, io.bdeploy/demo/client-app/windows:1.0.0, ...]}"}

      Attachments

        Issue Links

          Activity

            People

              ckozak Carter Kozak
              mduft Markus Duft
              Votes:
              0 Vote for this issue
              Watchers:
              4 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: