Skip to content

Commit

Permalink
Verify log file rotation for periods longer than day
Browse files Browse the repository at this point in the history
  • Loading branch information
gtroitsk committed Aug 28, 2024
1 parent 9723ea1 commit e1262c6
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,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) {
Expand Down
5 changes: 5 additions & 0 deletions logging/jboss/src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
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.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;
import io.quarkus.test.scenarios.QuarkusScenario;
import io.quarkus.test.services.QuarkusApplication;
import io.quarkus.test.utils.FileUtils;

@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";

@QuarkusApplication
static RestService app = new RestService();

Expand All @@ -25,4 +43,31 @@ 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 {
Path logDir = app.getServiceFolder();

app.given().when().get("/log").then().statusCode(204);

Awaitility.await().atMost(10, TimeUnit.SECONDS).until(() -> {
File[] rotatedFiles = logDir.toFile().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+$";
List<Path> rotatedFiles = Files.list(logDir)
.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 -> FileUtils.loadFile(rotatedFile.toFile()).lines())
.anyMatch(line -> line.contains(EXPECTED_LOG_MESSAGE));

assertTrue(messageFound, "Rotated log files do not contain the expected message " + EXPECTED_LOG_MESSAGE);
}
}

0 comments on commit e1262c6

Please sign in to comment.