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

TimeBasedTriggeringPolicy of RollingRandomAccessFile is not updated instantly

    XMLWordPrintableJSON

Details

    Description

      If a RollingRandomAccessFile is updated with a new TimeBasedTriggeringPolicy during runtime, the new value is not updated until the original interval is reached once.

       

      E.g. if logging is initialized with the rule to rotate every day and is later rerconfigured to rotate every hour, then you have to wait until the next day in order to get hourly rotated files.

      This has to do with the ordering in the Update-Method of RollingRandomAccessFileManager. Before the new PatternProcessor is created the RollingRandomAccessFileManager initializes his TriggeringPolicy from the former PatternProcessor.

      org.apache.logging.log4j.core.appender.rolling.RollingRandomAccessFileManager:

      public void updateData(final Object data) {
        final FactoryData factoryData = (FactoryData) data;
        setRolloverStrategy(factoryData.getRolloverStrategy());
        setTriggeringPolicy(factoryData.getTriggeringPolicy()); // -> TimeBasedTriggeringPolicy.initialize will use old PatternProcessor
        setPatternProcessor(new PatternProcessor(factoryData.getPattern(), getPatternProcessor()));
      }
      

      org.apache.logging.log4j.core.appender.rolling.TimeBasedTriggeringPolicy:

      public void initialize(final RollingFileManager aManager) {
        this.manager = aManager;
        long current = aManager.getFileTime();
        if (current == 0) {
          current = System.currentTimeMillis();
        }  // LOG4J2-531: call getNextTime twice to force initialization of both prevFileTime and nextFileTime
        aManager.getPatternProcessor().getNextTime(current, interval, modulate);
        aManager.getPatternProcessor().setTimeBased(true);
      
        nextRolloverMillis = ThreadLocalRandom.current().nextLong(0, 1 + maxRandomDelayMillis)
              + aManager.getPatternProcessor().getNextTime(current, interval, modulate); // -> time comes from the old PatternProcessor (see above: RollingRandomAccessFileManager.updateData)
      }
      

      See also the attached simple testcase.

      Attachments

        1. testcase.zip
          2 kB
          Heiko Schwanke

        Issue Links

          Activity

            People

              rgoers Ralph Goers
              HeikoS Heiko Schwanke
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: