diff --git a/src/main/java/org/jboss/logmanager/handlers/PeriodicRotatingFileHandler.java b/src/main/java/org/jboss/logmanager/handlers/PeriodicRotatingFileHandler.java index 5efa6e5b..4884e1f7 100644 --- a/src/main/java/org/jboss/logmanager/handlers/PeriodicRotatingFileHandler.java +++ b/src/main/java/org/jboss/logmanager/handlers/PeriodicRotatingFileHandler.java @@ -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; @@ -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); diff --git a/src/test/java/org/jboss/logmanager/handlers/PeriodicRotatingFileHandlerTests.java b/src/test/java/org/jboss/logmanager/handlers/PeriodicRotatingFileHandlerTests.java index 0f927cda..163710dd 100644 --- a/src/test/java/org/jboss/logmanager/handlers/PeriodicRotatingFileHandlerTests.java +++ b/src/test/java/org/jboss/logmanager/handlers/PeriodicRotatingFileHandlerTests.java @@ -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; @@ -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 @@ -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(); @@ -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()); @@ -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()); @@ -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; + } }