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

SizeAndTimeBasedFNATP will overwrite previous logging files when File property is set at RollingFileAppender

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Fixed
    • Affects Version/s: None
    • Fix Version/s: None
    • Component/s: logback-core
    • Labels:
      None

      Description

      Here's the failure case:

      Configuration:
      <appender name="appender" class="ch.qos.logback.core.rolling.RollingFileAppender">
      <File>c:/var/tmp/base.log</File>
      <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
      <FileNamePattern>
      c:/var/tmp/%d

      {yyyy-MM-dd_HH}

      .%i.log
      </FileNamePattern>
      <TimeBasedFileNamingAndTriggeringPolicy
      class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
      <MaxFileSize>1KB</MaxFileSize>
      </TimeBasedFileNamingAndTriggeringPolicy>
      </rollingPolicy>
      <layout class="ch.qos.logback.classic.PatternLayout">
      <Pattern>
      %d

      {HH:mm:ss.SSS}

      [%thread] %-5level %logger

      {36}

      - %msg%n
      </Pattern>
      </layout>
      </appender>

      Steps to reproduce:
      1. Start the application
      2. base.log was created.
      3. yyyy-MM-dd_HH.0.log was created on rollover

      4. rebounce the application
      5. log to base.log
      6. yyyy-MM-dd_HH.0.log was created on rollover, overwrite the previous yyyy-MM-dd_HH.0.log. Logging messages are lost.

      It looks like the root cause is that SizeAndtimeBasedFNATP does not update the currentPeriodsCounter when parentsRawFileProperty is set.
      if (tbrp.getParentsRawFileProperty() == null)

      { String regex = tbrp.fileNamePattern.toRegex(dateInCurrentPeriod); String stemRegex = FileFilterUtil.afterLastSlash(regex); computeCurrentPeriodsHighestCounterValue(stemRegex); }

      SizeAndTimeBasedFNATP should update currentPeriodsCounter regardless whether parentsRawFileProperty is set or not.
      Here's a fix works for me.
      SizeAndTimeBasedFNATP.java
      @Override
      public void start() {
      // we depend on certain fields having been initialized
      // in super.start()
      super.start();

      archiveRemover = new SizeAndTimeBasedArchiveRemover(tbrp.fileNamePattern, rc);
      archiveRemover.setContext(context);

      // we need to get the correct value of currentPeriodsCounter.
      // usually the value is 0, unless the appender or the application
      // is stopped and restarted within the same period
      //if (tbrp.getParentsRawFileProperty() == null)

      { String regex = tbrp.fileNamePattern.toRegex(dateInCurrentPeriod); String stemRegex = FileFilterUtil.afterLastSlash(regex); computeCurrentPeriodsHighestCounterValue(stemRegex); //}

      started = true;
      }

      void computeCurrentPeriodsHighestCounterValue(final String stemRegex) {
      File file = new File(getCurrentPeriodsFileNameWithoutCompressionSuffix());

      File parentDir = file.getParentFile();
      File[] matchingFileArray = FileFilterUtil
      .filesInFolderMatchingStemRegex(parentDir, stemRegex);

      if (matchingFileArray == null || matchingFileArray.length == 0)

      { return; }

      FileFilterUtil.reverseSortFileArrayByName(matchingFileArray);
      currentPeriodsCounter = FileFilterUtil.extractCounter(matchingFileArray[0], stemRegex);

      //If parentsRawFileProperty is set, we should increment currentPeriodsCounter by one in order to avoid overwrite the last archive file.
      if(tbrp.getParentsRawFileProperty() != null)

      { currentPeriodsCounter ++; }

      }

      Thanks,
      Tom

        Attachments

          Activity

            People

            • Assignee:
              ceki Ceki Gülcü
              Reporter:
              tomliliu tomliliu
            • Votes:
              1 Vote for this issue
              Watchers:
              1 Start watching this issue

              Dates

              • Created:
                Updated:
                Resolved: