From e98b116f351925d9d16010d6182034baa34ec83c Mon Sep 17 00:00:00 2001 From: Georgii Troitskii Date: Mon, 26 Aug 2024 00:00:39 +0200 Subject: [PATCH] Verify log file rotation for periods longer than day https://github.com/quarkusio/quarkus/issues/40016 --- .../quarkus/ts/logging/jboss/LogResource.java | 5 ++ .../src/main/resources/application.properties | 5 ++ .../logging/jboss/DefaultMinLogLevelIT.java | 52 +++++++++++++++++++ 3 files changed, 62 insertions(+) diff --git a/logging/jboss/src/main/java/io/quarkus/ts/logging/jboss/LogResource.java b/logging/jboss/src/main/java/io/quarkus/ts/logging/jboss/LogResource.java index 06d2164caa..b7bf5333d4 100644 --- a/logging/jboss/src/main/java/io/quarkus/ts/logging/jboss/LogResource.java +++ b/logging/jboss/src/main/java/io/quarkus/ts/logging/jboss/LogResource.java @@ -1,5 +1,6 @@ package io.quarkus.ts.logging.jboss; +import jakarta.enterprise.context.ApplicationScoped; import jakarta.inject.Inject; import jakarta.ws.rs.GET; import jakarta.ws.rs.POST; @@ -51,6 +52,10 @@ public void logExample() { LOG.info("Info log example"); LOG.debug("Debug log example"); LOG.trace("Trace log example"); + + for (int i = 0; i < 10; i++) { + LOG.info("Example log message: " + i); + } } private void addLogMessage(Logger logger, String level, String message) { diff --git a/logging/jboss/src/main/resources/application.properties b/logging/jboss/src/main/resources/application.properties index 636d32f2b9..98b089a58c 100644 --- a/logging/jboss/src/main/resources/application.properties +++ b/logging/jboss/src/main/resources/application.properties @@ -8,3 +8,8 @@ # By default min-level is set to DEBUG #quarkus.log.min-level=DEBUG quarkus.log.level=TRACE + +quarkus.log.file.enable=true +quarkus.log.file.rotation.file-suffix=.yyyy-MM +quarkus.log.file.rotation.max-file-size=100 +quarkus.log.file.rotation.max-backup-index=2 diff --git a/logging/jboss/src/test/java/io/quarkus/ts/logging/jboss/DefaultMinLogLevelIT.java b/logging/jboss/src/test/java/io/quarkus/ts/logging/jboss/DefaultMinLogLevelIT.java index 7647529e66..aad9dc0520 100644 --- a/logging/jboss/src/test/java/io/quarkus/ts/logging/jboss/DefaultMinLogLevelIT.java +++ b/logging/jboss/src/test/java/io/quarkus/ts/logging/jboss/DefaultMinLogLevelIT.java @@ -1,5 +1,19 @@ package io.quarkus.ts.logging.jboss; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.io.File; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.util.List; +import java.util.concurrent.TimeUnit; + +import org.awaitility.Awaitility; import org.junit.jupiter.api.Test; import io.quarkus.test.bootstrap.RestService; @@ -9,6 +23,11 @@ @QuarkusScenario public class DefaultMinLogLevelIT { + private static final String LOG_FILE_NAME = "quarkus.log"; + private static final DateTimeFormatter DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM"); + private static final String EXPECTED_LOG_MESSAGE = "Example log message"; + private final String LOG_DIR = "target/" + this.getClass().getSimpleName() + "/app"; + @QuarkusApplication static RestService app = new RestService(); @@ -25,4 +44,37 @@ public void checkDefaultLogMinLevel() { // the value of minimum logging level overrides the logging level app.logs().assertDoesNotContain("Trace log example"); } + + // Verifies https://github.com/quarkusio/quarkus/issues/40016 + @Test + public void checkLogRotationContent() throws IOException { + app.given().when().get("/log").then().statusCode(204); + + Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> { + File logDir = new File(LOG_DIR); + File[] rotatedFiles = logDir.listFiles((dir, name) -> name.startsWith(LOG_FILE_NAME)); + return rotatedFiles != null && rotatedFiles.length > 1; + }); + + // Verify that the rotated file with the expected suffix exists + String expectedSuffix = "." + DATE_FORMATTER.format(LocalDate.now()) + "\\.\\d+$"; + Path logDirPath = Path.of(LOG_DIR); + List rotatedFiles = Files.list(logDirPath) + .filter(path -> path.getFileName().toString().matches(LOG_FILE_NAME + expectedSuffix)).toList(); + + assertFalse(rotatedFiles.isEmpty(), "Rotated log file with expected suffix not found."); + + // Check if any rotated file contains the log message + boolean messageFound = rotatedFiles.stream() + .flatMap(rotatedFile -> { + try { + return Files.lines(rotatedFile); + } catch (IOException e) { + throw new UncheckedIOException(e); + } + }) + .anyMatch(line -> line.contains(EXPECTED_LOG_MESSAGE)); + + assertTrue(messageFound, "Rotated log files do not contain the expected message " + EXPECTED_LOG_MESSAGE); + } }