Skip to content

Commit

Permalink
Merge pull request #232 from wuwen5/LOGBACK-894
Browse files Browse the repository at this point in the history
Fix for LOGBACK-894 cleanByPeriodOffset add time check
  • Loading branch information
tony19 committed Jan 10, 2015
2 parents 47b0a1f + eb501c5 commit a9ca410
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
Expand Up @@ -39,7 +39,12 @@ public void cleanByPeriodOffset(Date now, int periodOffset) {
parentDir, stemRegex);

for (File f : matchingFileArray) {
f.delete();
Date fileLastModified = rc.getRelativeDate(new Date(f.lastModified()), -1);

if (fileLastModified.compareTo(dateOfPeriodToClean) <= 0) {
addInfo("deleting " + f);
f.delete();
}
}

if (parentClean) {
Expand Down
Expand Up @@ -28,11 +28,17 @@ protected void cleanByPeriodOffset(Date now, int periodOffset) {
String filename = fileNamePattern.convert(date2delete);
File file2Delete = new File(filename);
if (file2Delete.exists() && file2Delete.isFile()) {
file2Delete.delete();
addInfo("deleting " + file2Delete);
if (parentClean) {
removeFolderIfEmpty(file2Delete.getParentFile());
Date fileLastModified = rc.getRelativeDate(new Date(file2Delete.lastModified()), -1);

if (fileLastModified.compareTo(date2delete) <= 0) {
addInfo("deleting " + file2Delete);
file2Delete.delete();

if (parentClean) {
removeFolderIfEmpty(file2Delete.getParentFile());
}
}

}
}

Expand Down
Expand Up @@ -224,6 +224,31 @@ public void cleanHistoryOnStart() {
check(expectedCountWithoutFolders(maxHistory));
}

@Test
public void cleanHistoryOnStartWithDayPattern() {
long now = this.currentTime;
String fileNamePattern = randomOutputDir + "clean-%d{dd}.txt";
int maxHistory = 3;
for (int i = 0; i <= 5; i++) {
logOncePeriod(now, fileNamePattern, maxHistory);
now = now + MILLIS_IN_DAY;
}
StatusPrinter.print(context);
check(expectedCountWithoutFolders(maxHistory));
}

This comment has been minimized.

Copy link
@ceki

ceki Jan 12, 2015

Member

The problem is not with the pattern. Thus, I don't think it is useful to repeat this the same test varying only the roll over frequency. I would keep only one test removing the other.

@Test
public void cleanHistoryOnStartWithHourPattern() {
long now = this.currentTime;
String fileNamePattern = randomOutputDir + "clean-%d{HH}.txt";
int maxHistory = 3;
for (int i = 0; i <= 5; i++) {
logOncePeriod(now, fileNamePattern, maxHistory);
now = now + MILLIS_IN_HOUR;
}
StatusPrinter.print(context);
check(expectedCountWithoutFolders(maxHistory));
}

int expectedCountWithoutFolders(int maxHistory) {
return maxHistory + 1;
Expand Down

0 comments on commit a9ca410

Please sign in to comment.