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

OnStartupTriggeringPolicy only rolls over when minsize = 0 with DirectWriteTriggeringPolicy

    XMLWordPrintableJSON

Details

    Description

      I am trying to use a RollingFileAppender with a CompositeTriggeringPolicy that includes a OnStartupTriggeringPolicy which is deemed to rollover the log when the JVM is (re-)started.

      However, I have noted that the files only roll over on startup when I specify "minsize = 0" on the log4j2.xml configuration file. If I put any other value or leave the minsize unspecified (i.e. rely on its default value minsize = 1), the appender proceeds writing to the previously existing file.

      I have run the debugger and traced down the bug according as follows.

      All the logic in OnStartupTriggeringPolicy.initialize() is conditioned upon the size of the rolling file being larger than minSize

          public void initialize(final RollingFileManager manager) {
              if (manager.getFileTime() < JVM_START_TIME && manager.getFileSize() >= minSize) {
                 ...
              }
          }
      

      However, manager.getFileSize() always returns 0, thus the body of that if is never entered (... unless minSize == 0). In fact, this call to OnStartupTriggeringPolicy.initialize() is invoked indeed from RollingFileManager.initialize() before any value has been assigned to size

          public void initialize() {
      
              if (!initialized) {
                  LOGGER.debug("Initializing triggering policy {}", triggeringPolicy);
                  initialized = true;
                  triggeringPolicy.initialize(this);
                  if (triggeringPolicy instanceof LifeCycle) {
                      ((LifeCycle) triggeringPolicy).start();
                  }
                  if (directWrite) {
                      // LOG4J2-2485: Initialize size from the most recently written file.
                      File file = new File(getFileName());
                      if (file.exists()) {
                          size = file.length();
                      } else {
                          ((DirectFileRolloverStrategy) rolloverStrategy).clearCurrentFileName();
                      }
                  }
              }
          }
      

      Thus, within OnStartupTriggeringPolicy.initialize(), manager.getFileSize() always evaluates to 0, and the only way that the file is rolled over at startup is that minSize has been configured as 0.

      As a side consequence, this may also affect SizeBaseTriggerinPolicy if a rollover is needed by coincidence during startup.

      Attachments

        Issue Links

          Activity

            People

              rgoers Ralph Goers
              samuelm Samuel Martin
              Votes:
              0 Vote for this issue
              Watchers:
              3 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved: