19
19
20
20
import java .io .File ;
21
21
import java .io .FileFilter ;
22
+ import java .io .IOException ;
22
23
import java .time .Instant ;
23
24
import java .time .ZoneId ;
24
25
import java .time .ZonedDateTime ;
@@ -338,10 +339,10 @@ public void dailyChronologSizeBasedRolloverWithSecondPhase() {
338
339
checkDirPatternCompliance (maxHistory + 1 );
339
340
}
340
341
341
- void logTwiceAndStop (long currentTime , String fileNamePattern , int maxHistory , long durationInMillis ) {
342
+ void logTwiceAndStop (long currentTime , String fileNamePattern , int maxHistory , long durationInMillis , boolean cleanLogsByLastModifiedDate ) {
342
343
ConfigParameters params = new ConfigParameters (currentTime ).fileNamePattern (fileNamePattern )
343
344
.maxHistory (maxHistory );
344
- buildRollingFileAppender (params , DO_CLEAN_HISTORY_ON_START );
345
+ buildRollingFileAppender (params , DO_CLEAN_HISTORY_ON_START , cleanLogsByLastModifiedDate );
345
346
rfa .doAppend ("Hello ----------------------------------------------------------" + new Date (currentTime ));
346
347
currentTime += durationInMillis / 2 ;
347
348
add (tbrp .compressionFuture );
@@ -359,7 +360,7 @@ public void cleanHistoryOnStartWithHourPattern() {
359
360
String fileNamePattern = randomOutputDir + "clean-%d{" + DAILY_HOUR_PATTERN + "}.txt" ;
360
361
int maxHistory = 3 ;
361
362
for (int i = 0 ; i <= 5 ; i ++) {
362
- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR );
363
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
363
364
simulatedTime += MILLIS_IN_HOUR ;
364
365
}
365
366
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -379,7 +380,7 @@ public void cleanHistoryOnStartWithHourPatternWithCollisions() {
379
380
String fileNamePattern = randomOutputDir + "clean-%d{HH}.txt" ;
380
381
int maxHistory = 3 ;
381
382
for (int i = 0 ; i <= 5 ; i ++) {
382
- logTwiceAndStop (now , fileNamePattern , maxHistory , MILLIS_IN_DAY );
383
+ logTwiceAndStop (now , fileNamePattern , maxHistory , MILLIS_IN_DAY , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
383
384
now = now + MILLIS_IN_HOUR ;
384
385
}
385
386
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -391,7 +392,7 @@ public void cleanHistoryOnStartWithDayPattern() {
391
392
String fileNamePattern = randomOutputDir + "clean-%d{" + DAILY_DATE_PATTERN + "}.txt" ;
392
393
int maxHistory = 3 ;
393
394
for (int i = 0 ; i <= 5 ; i ++) {
394
- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_DAY );
395
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_DAY , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
395
396
simulatedTime += MILLIS_IN_DAY ;
396
397
}
397
398
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -403,7 +404,26 @@ public void cleanHistoryOnStartWithHourDayPattern() {
403
404
String fileNamePattern = randomOutputDir + "clean-%d{yyyy-MM-dd-HH}.txt" ;
404
405
int maxHistory = 3 ;
405
406
for (int i = 0 ; i <= 5 ; i ++) {
406
- logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR );
407
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_NOT_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
408
+ simulatedTime += MILLIS_IN_HOUR ;
409
+ }
410
+ checkFileCount (expectedCountWithoutFolders (maxHistory ));
411
+ }
412
+
413
+ @ Test
414
+ public void cleanLogsByLastModifiedDateWithHourDayPattern () throws IOException {
415
+ long simulatedTime = WED_2016_03_23_T_230705_CET ;
416
+ String fileNamePattern = randomOutputDir + "clean-%d{yyyy-MM-dd-HH}.txt" ;
417
+ int maxHistory = 3 ;
418
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
419
+ simulatedTime += MILLIS_IN_HOUR ;
420
+ File fileToDelete = new File (randomOutputDir + "clean-0000-00-00-00.txt" );
421
+ if (!fileToDelete .exists ()) {
422
+ assertTrue (fileToDelete .createNewFile ());
423
+ }
424
+ assertTrue (fileToDelete .setLastModified (0 ));
425
+ for (int i = 0 ; i <= 2 ; i ++) {
426
+ logTwiceAndStop (simulatedTime , fileNamePattern , maxHistory , MILLIS_IN_HOUR , DO_CLEAN_LOGS_BY_LAST_MODIFIED_DATE );
407
427
simulatedTime += MILLIS_IN_HOUR ;
408
428
}
409
429
checkFileCount (expectedCountWithoutFolders (maxHistory ));
@@ -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