Uploaded image for project: 'logback'
  1. logback
  2. LOGBACK-92

TimeBasedRollingPolicy does not trigger at application startup - MaxHistory not working

    XMLWordPrintable

Details

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: Major Major
    • None
    • None
    • logback-core
    • None
    • Windows XP, Java 1.6.0_27

    Description

      I am using the RollingFileAppender with TimeBasedRolliingPolicy. The application is a desktop utility that may be run several times per day but is almost never running at midnight when the roll would normally occur.
      The log file naming is working correctly - each day's file is date stamped, but the MaxHistory is not working at all since it is not enforced at app startup.

      Pertinent config section:
      <appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <fileNamePattern>MyApp_%d.log</fileNamePattern> <!-- Daily rollover -->
      <maxHistory>7</maxHistory> <!-- keep 1 week history -->
      </rollingPolicy>
      <encoder>
      <pattern>%date %-5level [%thread] %logger

      {10}

      %msg%n</pattern>
      </encoder>
      </appender>

      I found what I believe is the source of the issue in TimeBasedFileNamingAndTriggeringPolicyBase:
      public void start() {
      ...
      setDateInCurrentPeriod(new Date(getCurrentTime()));
      if (tbrp.getParentsRawFileProperty() != null) {
      File currentFile = new File(tbrp.getParentsRawFileProperty());
      if (currentFile.exists() && currentFile.canRead())

      { setDateInCurrentPeriod(new Date(currentFile.lastModified())); }

      }

      addInfo("Setting initial period to " + dateInCurrentPeriod);
      computeNextCheck();
      }

      Since there is no parentsRawFileProperty, this uses the current time and sets the next check at midnight tonight. Therefore the check is not run at startup, and in my case is never run. This would work fine if dateInCurrentPeriod was set to anything before today. I would suggest the following change:

      public void start() {
      ...
      File currentFile = new File(tbrp.getActiveFileName()); // Use same logic if ParentsRawFileProperty is set or not.
      if (currentFile.exists() && currentFile.canRead())

      { setDateInCurrentPeriod(new Date(currentFile.lastModified())); }

      else

      { setDateInCurrentPeriod(0); // No current file found, force a triggering event // or if the above is too brute-force: setDateInCurrentPeriod(rc.getRelativeDate(new Date(), -1)); }

      addInfo("Setting initial period to " + dateInCurrentPeriod);
      computeNextCheck();
      }

      Note - this was originally reported as a comment on LBCORE-147; moved here to be tracked as a separate issue.

      Thanks,
      -Bruce

      Attachments

        Activity

          People

            ceki Ceki Gülcü
            birving Bruce E. Irving
            Votes:
            0 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: