Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify log file rotation for periods longer than day #1946

Merged
merged 1 commit into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 6 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,9 @@
# 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
# Max file size value is set to this value to ensure that both backup logs are created
quarkus.log.file.rotation.max-file-size=100
jedla97 marked this conversation as resolved.
Show resolved Hide resolved
quarkus.log.file.rotation.max-backup-index=2
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
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.Tag;
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 +44,35 @@ public void checkDefaultLogMinLevel() {
// the value of minimum logging level overrides the logging level
app.logs().assertDoesNotContain("Trace log example");
}

@Test
@Tag("https://github.com/quarkusio/quarkus/issues/40016")
public void checkLogRotationContent() throws IOException {
Path logDir = app.getServiceFolder();
// one main log file plus others rotated
int logFilesNumber = Integer.parseInt(app.getProperty("quarkus.log.file.rotation.max-backup-index").get()) + 1;

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 == logFilesNumber;
});

// 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();
michalvavrik marked this conversation as resolved.
Show resolved Hide resolved

assertFalse(rotatedFiles.isEmpty(), "Rotated log files with expected suffix were not found.");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For record @jedla97 , I still think this condition doesn't make sense. First there is waiting for max-backup-index + 1=3 and now it's satisfied with 1


// Check if rotated files contain the log message
boolean allFilesContainMessage = rotatedFiles.stream()
.allMatch(rotatedFile -> {
String fileContent = FileUtils.loadFile(rotatedFile.toFile());
return fileContent.contains(EXPECTED_LOG_MESSAGE);
});

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