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

JsonTemplateLayout epoch resolver renders incorrect values

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Fixed
    • 2.14.0, 2.14.1
    • 2.15.0
    • JsonTemplateLayout
    • None

    Description

      JTL epoch resolver renders the same timestamp repeatedly. See the following application using log4j-layout-template-json 2.14.1:

      public static void main(String[] args) throws InterruptedException {
          initLog4j();
          Logger logger = LogManager.getLogger();
          for (int i = 0; i < 5; i++) {
              logger.info("{}", i);
              Thread.sleep(100);
          }
      }
      
      private static LoggerContext initLog4j() {
      
          // Create the configuration builder.
          ConfigurationBuilder<BuiltConfiguration> configBuilder = ConfigurationBuilderFactory
                  .newConfigurationBuilder()
                  .setStatusLevel(Level.ERROR)
                  .setConfigurationName(TimeDriftDemo.class.getSimpleName());
      
          // Create the configuration.
          String appenderName = "Console";
          String eventTemplate = "" +
                  "[{\"$resolver\":\"timestamp\",\"epoch\":{\"unit\":\"nanos\"}}" +
                  ",{\"$resolver\":\"timestamp\",\"pattern\":{\"format\":\"ss.SSS'Z'\",\"timeZone\":\"UTC\",\"locale\":\"en_US\"}}" +
                  "]";
          Configuration config = configBuilder
                  .add(configBuilder
                          .newAppender(appenderName, "Console")
                          .add(configBuilder
                                  .newLayout("JsonTemplateLayout")
                                  .addAttribute(
                                          "eventTemplate",
                                          eventTemplate)))
                  .add(configBuilder.newLogger("com.vlkan", Level.TRACE))
                  .add(configBuilder
                          .newRootLogger(Level.ERROR)
                          .add(configBuilder.newAppenderRef(appenderName)))
                  .build(false);
      
          // Initialize the configuration.
          return Configurator.initialize(config);
      
      }
      

      Note the identical epochs in the output:

      [1635411617932465000,"17.932Z"]
      [1635411617932465000,"18.034Z"]
      [1635411617932465000,"18.134Z"]
      [1635411617932465000,"18.234Z"]
      [1635411617932465000,"18.335Z"]
      

      The issue was originally reported by Andrew Harris in a log4j-user post.

      Attachments

        Activity

          People

            vy Volkan Yazici
            vy Volkan Yazici
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: