Details
-
Bug
-
Resolution: Fixed
-
Major
-
None
-
None
-
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
%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())
}
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())
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
Issue Links
- depends on
-
LOGBACK-162 RollingPolicy MaxHistory not deleting old files
- Resolved