14
14
package ch .qos .logback .core .rolling ;
15
15
16
16
import static ch .qos .logback .core .CoreConstants .DAILY_DATE_PATTERN ;
17
- import static org .junit .jupiter .api .Assertions .assertEquals ;
18
- import static org .junit .jupiter .api .Assertions .assertTrue ;
17
+ import static org .junit .jupiter .api .Assertions .*;
19
18
20
19
import java .io .File ;
21
20
import java .io .FileFilter ;
21
+ import java .io .IOException ;
22
22
import java .time .Instant ;
23
23
import java .time .ZoneId ;
24
24
import java .time .ZonedDateTime ;
@@ -338,10 +338,10 @@ public void dailyChronologSizeBasedRolloverWithSecondPhase() {
338
338
checkDirPatternCompliance (maxHistory + 1 );
339
339
}
340
340
341
- void logTwiceAndStop (long currentTime , String fileNamePattern , int maxHistory , long durationInMillis ) {
341
+ void logTwiceAndStop (long currentTime , String fileNamePattern , int maxHistory , long durationInMillis , boolean cleanLogsByLastModifiedDate ) {
342
342
ConfigParameters params = new ConfigParameters (currentTime ).fileNamePattern (fileNamePattern )
343
343
.maxHistory (maxHistory );
344
- buildRollingFileAppender (params , DO_CLEAN_HISTORY_ON_START );
344
+ buildRollingFileAppender (params , DO_CLEAN_HISTORY_ON_START , cleanLogsByLastModifiedDate );
345
345
rfa .doAppend ("Hello ----------------------------------------------------------" + new Date (currentTime ));
346
346
currentTime += durationInMillis / 2 ;
347
347
add (tbrp .compressionFuture );
@@ -359,7 +359,7 @@ public void cleanHistoryOnStartWithHourPattern() {
359
359
String fileNamePattern = randomOutputDir + "clean-%d{" + DAILY_HOUR_PATTERN + "}.txt" ;
360
360
int maxHistory = 3 ;
361
361
for (int i = 0 ; i <= 5 ; i ++) {
362
- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR );
362
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
363
363
simulatedTime += MILLIS_IN_HOUR ;
364
364
}
365
365
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -379,7 +379,7 @@ public void cleanHistoryOnStartWithHourPatternWithCollisions() {
379
379
String fileNamePattern = randomOutputDir + "clean-%d{HH}.txt" ;
380
380
int maxHistory = 3 ;
381
381
for (int i = 0 ; i <= 5 ; i ++) {
382
- logTwiceAndStop (now , fileNamePattern , maxHistory , MILLIS_IN_DAY );
382
+ logTwiceAndStop (now , fileNamePattern , maxHistory , MILLIS_IN_DAY , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
383
383
now = now + MILLIS_IN_HOUR ;
384
384
}
385
385
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -391,7 +391,7 @@ public void cleanHistoryOnStartWithDayPattern() {
391
391
String fileNamePattern = randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt" ;
392
392
int maxHistory = 3 ;
393
393
for (int i = 0 ; i <= 5 ; i ++) {
394
- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_DAY );
394
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_DAY , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
395
395
simulatedTime += MILLIS_IN_DAY ;
396
396
}
397
397
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -403,12 +403,32 @@ public void cleanHistoryOnStartWithHourDayPattern() {
403
403
String fileNamePattern = randomOutputDir + "clean-%d{yyyy-MM-dd-HH}.txt" ;
404
404
int maxHistory = 3 ;
405
405
for (int i = 0 ; i <= 5 ; i ++) {
406
- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR );
406
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
407
407
simulatedTime += MILLIS_IN_HOUR ;
408
408
}
409
409
checkFileCount (expectedCountWithoutFolders (maxHistory ));
410
410
}
411
411
412
+ @ Test
413
+ public void cleanLogsByLastModifiedDateWithHourDayPattern () throws IOException {
414
+ long simulatedTime = WED_2016_03_23_T_230705_CET ;
415
+ String fileNamePattern = randomOutputDir + "clean-%d{yyyy-MM-dd-HH}.txt" ;
416
+ int maxHistory = 3 ;
417
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
418
+ simulatedTime += MILLIS_IN_HOUR ;
419
+ File fileToDelete = new File (randomOutputDir + "clean-0000-00-00-00.txt" );
420
+ if (!fileToDelete .exists ()) {
421
+ assertTrue (fileToDelete .createNewFile ());
422
+ }
423
+ assertTrue (fileToDelete .setLastModified (0 ));
424
+ for (int i = 0 ; i <= 2 ; i ++) {
425
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
426
+ simulatedTime += MILLIS_IN_HOUR ;
427
+ }
428
+ checkFileCount (expectedCountWithoutFolders (maxHistory ));
429
+ assertFalse (fileToDelete .exists ());
430
+ }
431
+
412
432
int expectedCountWithoutFolders (int maxHistory ) {
413
433
return maxHistory + 1 ;
414
434
}
@@ -422,7 +442,7 @@ int expectedCountWithFolders(int maxHistory, boolean withExtraFolder) {
422
442
return result ;
423
443
}
424
444
425
- void buildRollingFileAppender (ConfigParameters cp , boolean cleanHistoryOnStart ) {
445
+ void buildRollingFileAppender (ConfigParameters cp , boolean cleanHistoryOnStart , boolean cleanLogsByLastModifiedDate ) {
426
446
rfa .setContext (context );
427
447
rfa .setEncoder (encoder );
428
448
tbrp .setContext (context );
@@ -431,6 +451,7 @@ void buildRollingFileAppender(ConfigParameters cp, boolean cleanHistoryOnStart)
431
451
tbrp .setTotalSizeCap (new FileSize (cp .sizeCap ));
432
452
tbrp .setParent (rfa );
433
453
tbrp .setCleanHistoryOnStart (cleanHistoryOnStart );
454
+ tbrp .setCleanLogsByLastModifiedDate (cleanLogsByLastModifiedDate );
434
455
tbrp .timeBasedFileNamingAndTriggeringPolicy = tbfnatp ;
435
456
tbrp .timeBasedFileNamingAndTriggeringPolicy .setCurrentTime (cp .simulatedTime );
436
457
tbrp .start ();
@@ -440,10 +461,13 @@ void buildRollingFileAppender(ConfigParameters cp, boolean cleanHistoryOnStart)
440
461
441
462
boolean DO_CLEAN_HISTORY_ON_START = true ;
442
463
boolean DO_NOT_CLEAN_HISTORY_ON_START = false ;
464
+ boolean DO_CLEAN_LOGS_BY_LAST_MODIFIED_DATE = true ;
465
+ boolean DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE = false ;
466
+
443
467
444
468
long logOverMultiplePeriods (ConfigParameters cp ) {
445
469
446
- buildRollingFileAppender (cp , DO_NOT_CLEAN_HISTORY_ON_START );
470
+ buildRollingFileAppender (cp , DO_NOT_CLEAN_HISTORY_ON_START , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
447
471
448
472
int runLength = cp .simulatedNumberOfPeriods * ticksPerPeriod ;
449
473
int startInactivityIndex = cp .startInactivity * ticksPerPeriod ;
0 commit comments