Details
-
Bug
-
Resolution: Unresolved
-
Minor
-
None
-
1.0.9
-
not env specific.
Description
I tried to create a logback configuration for logging a new file every hour and keeping the last three hours worth of logfiles. Problem is that when restarting my app the old log files are deleted even though they are not yet 3 hours old.
This is my appender:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/tmp/logback-%d
.log</fileNamePattern>
<append>true</append>
<cleanHistoryOnStart>true</cleanHistoryOnStart>
<maxHistory>3</maxHistory>
</rollingPolicy>
<encoder>
<pattern>%-4relative [%thread] %-5level %class %line - %msg%n</pattern>
</encoder>
</appender>
The problem is that the DefaultArchiveRemover will on first clean (when lastHeartBeat == UNINITIALIZED) go back 14 * 24 hours and delete the old files. But since the filename only contains the hour, it will cycle through all values 0-23 and delete all the logfiles, new and old:
If clean runs first time at 12:00:00, it will delete like this:
/tmp/logback-08.log (09 through 11 preserved by the periodOffsetForDeletionTarget offset)
/tmp/logback-07.log
/tmp/logback-06.log
/tmp/logback-05.log
/tmp/logback-04.log
/tmp/logback-03.log
/tmp/logback-02.log
/tmp/logback-01.log
/tmp/logback-00.log
/tmp/logback-23.log
/tmp/logback-22.log
/tmp/logback-21.log
/tmp/logback-20.log
/tmp/logback-19.log
/tmp/logback-18.log
/tmp/logback-17.log
/tmp/logback-16.log
/tmp/logback-15.log
/tmp/logback-14.log
/tmp/logback-13.log
/tmp/logback-12.log --> the newest logfile!
/tmp/logback-11.log --> and these three
/tmp/logback-10.log --> which should be
/tmp/logback-09.log --> preserved by maxHistory=3