1616import static ch .qos .logback .core .CoreConstants .DAILY_DATE_PATTERN ;
1717import static org .junit .jupiter .api .Assertions .assertEquals ;
1818import static org .junit .jupiter .api .Assertions .assertTrue ;
19+ import static org .junit .jupiter .api .Assertions .assertFalse ;
1920
2021import java .io .File ;
2122import java .io .FileFilter ;
23+ import java .io .IOException ;
2224import java .time .Instant ;
2325import java .time .ZoneId ;
2426import java .time .ZonedDateTime ;
@@ -338,10 +340,10 @@ public void dailyChronologSizeBasedRolloverWithSecondPhase() {
338340 checkDirPatternCompliance (maxHistory + 1 );
339341 }
340342
341- void logTwiceAndStop (long currentTime , String fileNamePattern , int maxHistory , long durationInMillis ) {
343+ void logTwiceAndStop (long currentTime , String fileNamePattern , int maxHistory , long durationInMillis , boolean cleanLogsByLastModifiedDate ) {
342344 ConfigParameters params = new ConfigParameters (currentTime ).fileNamePattern (fileNamePattern )
343345 .maxHistory (maxHistory );
344- buildRollingFileAppender (params , DO_CLEAN_HISTORY_ON_START );
346+ buildRollingFileAppender (params , DO_CLEAN_HISTORY_ON_START , cleanLogsByLastModifiedDate );
345347 rfa .doAppend ("Hello ----------------------------------------------------------" + new Date (currentTime ));
346348 currentTime += durationInMillis / 2 ;
347349 add (tbrp .compressionFuture );
@@ -359,7 +361,7 @@ public void cleanHistoryOnStartWithHourPattern() {
359361 String fileNamePattern = randomOutputDir + "clean-%d{" + DAILY_HOUR_PATTERN + "}.txt" ;
360362 int maxHistory = 3 ;
361363 for (int i = 0 ; i <= 5 ; i ++) {
362- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR );
364+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
363365 simulatedTime += MILLIS_IN_HOUR ;
364366 }
365367 checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -379,7 +381,7 @@ public void cleanHistoryOnStartWithHourPatternWithCollisions() {
379381 String fileNamePattern = randomOutputDir + "clean-%d{HH}.txt" ;
380382 int maxHistory = 3 ;
381383 for (int i = 0 ; i <= 5 ; i ++) {
382- logTwiceAndStop (now , fileNamePattern , maxHistory , MILLIS_IN_DAY );
384+ logTwiceAndStop (now , fileNamePattern , maxHistory , MILLIS_IN_DAY , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
383385 now = now + MILLIS_IN_HOUR ;
384386 }
385387 checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -391,7 +393,7 @@ public void cleanHistoryOnStartWithDayPattern() {
391393 String fileNamePattern = randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt" ;
392394 int maxHistory = 3 ;
393395 for (int i = 0 ; i <= 5 ; i ++) {
394- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_DAY );
396+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_DAY , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
395397 simulatedTime += MILLIS_IN_DAY ;
396398 }
397399 checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -403,12 +405,32 @@ public void cleanHistoryOnStartWithHourDayPattern() {
403405 String fileNamePattern = randomOutputDir + "clean-%d{yyyy-MM-dd-HH}.txt" ;
404406 int maxHistory = 3 ;
405407 for (int i = 0 ; i <= 5 ; i ++) {
406- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR );
408+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
407409 simulatedTime += MILLIS_IN_HOUR ;
408410 }
409411 checkFileCount (expectedCountWithoutFolders (maxHistory ));
410412 }
411413
414+ @ Test
415+ public void cleanLogsByLastModifiedDateWithHourDayPattern () throws IOException {
416+ long simulatedTime = WED_2016_03_23_T_230705_CET ;
417+ String fileNamePattern = randomOutputDir + "clean-%d{yyyy-MM-dd-HH}.txt" ;
418+ int maxHistory = 3 ;
419+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
420+ simulatedTime += MILLIS_IN_HOUR ;
421+ File fileToDelete = new File (randomOutputDir + "clean-0000-00-00-00.txt" );
422+ if (!fileToDelete .exists ()) {
423+ assertTrue (fileToDelete .createNewFile ());
424+ }
425+ assertTrue (fileToDelete .setLastModified (0 ));
426+ for (int i = 0 ; i <= 2 ; i ++) {
427+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
428+ simulatedTime += MILLIS_IN_HOUR ;
429+ }
430+ checkFileCount (expectedCountWithoutFolders (maxHistory ));
431+ assertFalse (fileToDelete .exists ());
432+ }
433+
412434 int expectedCountWithoutFolders (int maxHistory ) {
413435 return maxHistory + 1 ;
414436 }
@@ -422,7 +444,7 @@ int expectedCountWithFolders(int maxHistory, boolean withExtraFolder) {
422444 return result ;
423445 }
424446
425- void buildRollingFileAppender (ConfigParameters cp , boolean cleanHistoryOnStart ) {
447+ void buildRollingFileAppender (ConfigParameters cp , boolean cleanHistoryOnStart , boolean cleanLogsByLastModifiedDate ) {
426448 rfa .setContext (context );
427449 rfa .setEncoder (encoder );
428450 tbrp .setContext (context );
@@ -431,6 +453,7 @@ void buildRollingFileAppender(ConfigParameters cp, boolean cleanHistoryOnStart)
431453 tbrp .setTotalSizeCap (new FileSize (cp .sizeCap ));
432454 tbrp .setParent (rfa );
433455 tbrp .setCleanHistoryOnStart (cleanHistoryOnStart );
456+ tbrp .setCleanLogsByLastModifiedDate (cleanLogsByLastModifiedDate );
434457 tbrp .timeBasedFileNamingAndTriggeringPolicy = tbfnatp ;
435458 tbrp .timeBasedFileNamingAndTriggeringPolicy .setCurrentTime (cp .simulatedTime );
436459 tbrp .start ();
@@ -440,10 +463,13 @@ void buildRollingFileAppender(ConfigParameters cp, boolean cleanHistoryOnStart)
440463
441464 boolean DO_CLEAN_HISTORY_ON_START = true ;
442465 boolean DO_NOT_CLEAN_HISTORY_ON_START = false ;
466+ boolean DO_CLEAN_LOGS_BY_LAST_MODIFIED_DATE = true ;
467+ boolean DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE = false ;
468+
443469
444470 long logOverMultiplePeriods (ConfigParameters cp ) {
445471
446- buildRollingFileAppender (cp , DO_NOT_CLEAN_HISTORY_ON_START );
472+ buildRollingFileAppender (cp , DO_NOT_CLEAN_HISTORY_ON_START , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
447473
448474 int runLength = cp .simulatedNumberOfPeriods * ticksPerPeriod ;
449475 int startInactivityIndex = cp .startInactivity * ticksPerPeriod ;
0 commit comments