16
16
import static ch .qos .logback .core .CoreConstants .DAILY_DATE_PATTERN ;
17
17
import static org .junit .jupiter .api .Assertions .assertEquals ;
18
18
import static org .junit .jupiter .api .Assertions .assertTrue ;
19
+ import static org .junit .jupiter .api .Assertions .assertFalse ;
19
20
20
21
import java .io .File ;
21
22
import java .io .FileFilter ;
23
+ import java .io .IOException ;
22
24
import java .time .Instant ;
23
25
import java .time .ZoneId ;
24
26
import java .time .ZonedDateTime ;
@@ -338,10 +340,10 @@ public void dailyChronologSizeBasedRolloverWithSecondPhase() {
338
340
checkDirPatternCompliance (maxHistory + 1 );
339
341
}
340
342
341
- void logTwiceAndStop (long currentTime , String fileNamePattern , int maxHistory , long durationInMillis ) {
343
+ void logTwiceAndStop (long currentTime , String fileNamePattern , int maxHistory , long durationInMillis , boolean cleanLogsByLastModifiedDate ) {
342
344
ConfigParameters params = new ConfigParameters (currentTime ).fileNamePattern (fileNamePattern )
343
345
.maxHistory (maxHistory );
344
- buildRollingFileAppender (params , DO_CLEAN_HISTORY_ON_START );
346
+ buildRollingFileAppender (params , DO_CLEAN_HISTORY_ON_START , cleanLogsByLastModifiedDate );
345
347
rfa .doAppend ("Hello ----------------------------------------------------------" + new Date (currentTime ));
346
348
currentTime += durationInMillis / 2 ;
347
349
add (tbrp .compressionFuture );
@@ -359,7 +361,7 @@ public void cleanHistoryOnStartWithHourPattern() {
359
361
String fileNamePattern = randomOutputDir + "clean-%d{" + DAILY_HOUR_PATTERN + "}.txt" ;
360
362
int maxHistory = 3 ;
361
363
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 );
363
365
simulatedTime += MILLIS_IN_HOUR ;
364
366
}
365
367
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -379,7 +381,7 @@ public void cleanHistoryOnStartWithHourPatternWithCollisions() {
379
381
String fileNamePattern = randomOutputDir + "clean-%d{HH}.txt" ;
380
382
int maxHistory = 3 ;
381
383
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 );
383
385
now = now + MILLIS_IN_HOUR ;
384
386
}
385
387
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -391,7 +393,7 @@ public void cleanHistoryOnStartWithDayPattern() {
391
393
String fileNamePattern = randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt" ;
392
394
int maxHistory = 3 ;
393
395
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 );
395
397
simulatedTime += MILLIS_IN_DAY ;
396
398
}
397
399
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -403,12 +405,32 @@ public void cleanHistoryOnStartWithHourDayPattern() {
403
405
String fileNamePattern = randomOutputDir + "clean-%d{yyyy-MM-dd-HH}.txt" ;
404
406
int maxHistory = 3 ;
405
407
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 );
407
409
simulatedTime += MILLIS_IN_HOUR ;
408
410
}
409
411
checkFileCount (expectedCountWithoutFolders (maxHistory ));
410
412
}
411
413
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
+
412
434
int expectedCountWithoutFolders (int maxHistory ) {
413
435
return maxHistory + 1 ;
414
436
}
@@ -422,7 +444,7 @@ int expectedCountWithFolders(int maxHistory, boolean withExtraFolder) {
422
444
return result ;
423
445
}
424
446
425
- void buildRollingFileAppender (ConfigParameters cp , boolean cleanHistoryOnStart ) {
447
+ void buildRollingFileAppender (ConfigParameters cp , boolean cleanHistoryOnStart , boolean cleanLogsByLastModifiedDate ) {
426
448
rfa .setContext (context );
427
449
rfa .setEncoder (encoder );
428
450
tbrp .setContext (context );
@@ -431,6 +453,7 @@ void buildRollingFileAppender(ConfigParameters cp, boolean cleanHistoryOnStart)
431
453
tbrp .setTotalSizeCap (new FileSize (cp .sizeCap ));
432
454
tbrp .setParent (rfa );
433
455
tbrp .setCleanHistoryOnStart (cleanHistoryOnStart );
456
+ tbrp .setCleanLogsByLastModifiedDate (cleanLogsByLastModifiedDate );
434
457
tbrp .timeBasedFileNamingAndTriggeringPolicy = tbfnatp ;
435
458
tbrp .timeBasedFileNamingAndTriggeringPolicy .setCurrentTime (cp .simulatedTime );
436
459
tbrp .start ();
@@ -440,10 +463,13 @@ void buildRollingFileAppender(ConfigParameters cp, boolean cleanHistoryOnStart)
440
463
441
464
boolean DO_CLEAN_HISTORY_ON_START = true ;
442
465
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
+
443
469
444
470
long logOverMultiplePeriods (ConfigParameters cp ) {
445
471
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 );
447
473
448
474
int runLength = cp .simulatedNumberOfPeriods * ticksPerPeriod ;
449
475
int startInactivityIndex = cp .startInactivity * ticksPerPeriod ;
0 commit comments