Skip to content

Commit

Permalink
Fix periodic file rotation by week, month, year.
Browse files Browse the repository at this point in the history
  • Loading branch information
alex-pumpkin authored and jamezp committed Apr 23, 2024
1 parent ddaffa9 commit 052c6f1
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.time.Instant;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoField;
import java.time.temporal.ChronoUnit;
import java.util.TimeZone;
import java.util.logging.ErrorManager;
Expand Down Expand Up @@ -248,13 +249,13 @@ private void calcNextRollover(final Instant fromTime) {
switch (period) {
default:
case YEAR:
zdt = zdt.truncatedTo(ChronoUnit.YEARS).plusYears(1);
zdt = zdt.withDayOfYear(1).truncatedTo(ChronoUnit.DAYS).plusYears(1);
break;
case MONTH:
zdt = zdt.truncatedTo(ChronoUnit.MONTHS).plusYears(1);
zdt = zdt.withDayOfMonth(1).truncatedTo(ChronoUnit.DAYS).plusMonths(1);
break;
case WEEK:
zdt = zdt.truncatedTo(ChronoUnit.WEEKS).plusWeeks(1);
zdt = zdt.with(ChronoField.DAY_OF_WEEK, 1).truncatedTo(ChronoUnit.DAYS).plusWeeks(1);
break;
case DAY:
zdt = zdt.truncatedTo(ChronoUnit.DAYS).plusDays(1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZonedDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Calendar;
Expand Down Expand Up @@ -58,11 +60,7 @@ public void createHandler() throws IOException {
logFile = resolvePath(FILENAME);
}
// Create the handler
handler = new PeriodicRotatingFileHandler(logFile.toFile(), rotateFormatter.toPattern(), false);
handler.setFormatter(FORMATTER);
// Set append to true to ensure the rotated file is overwritten
handler.setAppend(true);
handler.setErrorManager(AssertingErrorManager.of());
handler = createHandler(rotateFormatter.toPattern());
}

@AfterEach
Expand All @@ -78,6 +76,47 @@ public void testRotate() throws Exception {
testRotate(cal, rotatedFile);
}

@Test
public void testRotateEveryYear() throws Exception {
closeHandler();
String suffix = ".yyyy";
handler = createHandler(suffix);
final Calendar cal = Calendar.getInstance();
final Path rotatedFile = resolvePath(FILENAME + DateTimeFormatter.ofPattern(suffix).format(LocalDate.now()));
testRotate(cal, rotatedFile, Calendar.YEAR);
}

@Test
public void testRotateEveryMonth() throws Exception {
closeHandler();
String suffix = ".yyyy-MM";
handler = createHandler(suffix);
final Calendar cal = Calendar.getInstance();
final Path rotatedFile = resolvePath(FILENAME + DateTimeFormatter.ofPattern(suffix).format(LocalDate.now()));
testRotate(cal, rotatedFile, Calendar.MONTH);
}

@Test
public void testRotateEveryWeek() throws Exception {
closeHandler();
String suffix = ".yyyy-ww";
handler = createHandler(suffix);
final Calendar cal = Calendar.getInstance();
final Path rotatedFile = resolvePath(FILENAME + DateTimeFormatter.ofPattern(suffix).format(LocalDate.now()));
testRotate(cal, rotatedFile, Calendar.WEEK_OF_YEAR);
}

@Test
public void testRotateEveryHourOfDay() throws Exception {
closeHandler();
String suffix = ".yyyy-MM-dd.HH";
handler = createHandler(suffix);
final Calendar cal = Calendar.getInstance();
final Path rotatedFile = resolvePath(FILENAME + DateTimeFormatter.ofPattern(suffix)
.format(LocalDateTime.now()));
testRotate(cal, rotatedFile, Calendar.HOUR_OF_DAY);
}

@Test
public void testOverwriteRotate() throws Exception {
final Calendar cal = Calendar.getInstance();
Expand Down Expand Up @@ -145,8 +184,12 @@ record = createLogRecord(Level.INFO, "Date: %s", nextDate);
}

private void testRotate(final Calendar cal, final Path rotatedFile) throws Exception {
testRotate(cal, rotatedFile, Calendar.DAY_OF_MONTH);
}

private void testRotate(final Calendar cal, final Path rotatedFile, int calendarField) throws Exception {
final SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
final int currentDay = cal.get(Calendar.DAY_OF_MONTH);
final int currentDay = cal.get(calendarField);
final int nextDay = currentDay + 1;

final String currentDate = sdf.format(cal.getTime());
Expand All @@ -163,7 +206,7 @@ private void testRotate(final Calendar cal, final Path rotatedFile) throws Excep
Assertions.assertTrue(lines.get(0).contains(currentDate), "Expected the line to contain the date: " + currentDate);

// Create a new record, increment the day by one and validate
cal.add(Calendar.DAY_OF_MONTH, nextDay);
cal.set(calendarField, nextDay);
final String nextDate = sdf.format(cal.getTime());
record = createLogRecord(Level.INFO, "Date: %s", nextDate);
record.setMillis(cal.getTimeInMillis());
Expand Down Expand Up @@ -231,4 +274,14 @@ record = createLogRecord(Level.INFO, "Date: %s", thirdDay);
}
compareArchiveContents(rotated1, rotated2, logFile.getFileName().toString());
}

private PeriodicRotatingFileHandler createHandler(String suffix) throws IOException {
// Create the handler
PeriodicRotatingFileHandler handler = new PeriodicRotatingFileHandler(logFile.toFile(), suffix, false);
handler.setFormatter(FORMATTER);
// Set append to true to ensure the rotated file is overwritten
handler.setAppend(true);
handler.setErrorManager(AssertingErrorManager.of());
return handler;
}
}

0 comments on commit 052c6f1

Please sign in to comment.