From 190df7eb98d272356309e9677b7f3ac8f99f02df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Galder=20Zamarren=CC=83o?= Date: Fri, 24 Sep 2021 08:36:23 +0200 Subject: [PATCH] Apply min-level logging config to root logger #20066 --- .../runtime/logging/LoggingSetupRecorder.java | 9 +++- .../unset/LoggingMinLevelAboveTest.java | 2 +- .../unset/LoggingMinLevelByDefaultTest.java | 2 +- .../unset/LoggingMinLevelGlobalTest.java | 46 +++++++++++++++++++ .../unset/LoggingMinLevelPromoteTest.java | 2 +- ....java => SetCategoryRuntimeLogLevels.java} | 2 +- .../unset/SetGlobalRuntimeLogLevel.java | 21 +++++++++ 7 files changed, 79 insertions(+), 5 deletions(-) create mode 100644 integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelGlobalTest.java rename integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/{SetRuntimeLogLevels.java => SetCategoryRuntimeLogLevels.java} (86%) create mode 100644 integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetGlobalRuntimeLogLevel.java diff --git a/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java b/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java index e5c8930634a78..06e6a98f4dad4 100644 --- a/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java +++ b/core/runtime/src/main/java/io/quarkus/runtime/logging/LoggingSetupRecorder.java @@ -89,7 +89,14 @@ public void initializeLogging(LogConfig config, LogBuildTimeConfig buildConfig, final LogContext logContext = LogContext.getLogContext(); final Logger rootLogger = logContext.getLogger(""); - rootLogger.setLevel(config.level); + if (config.level.intValue() < buildConfig.minLevel.intValue()) { + log.warnf("Root log level %s set below minimum logging level %s, promoting it to %s", + config.level, buildConfig.minLevel, buildConfig.minLevel); + + rootLogger.setLevel(buildConfig.minLevel); + } else { + rootLogger.setLevel(config.level); + } ErrorManager errorManager = new OnlyOnceErrorManager(); final Map filters = config.filters; diff --git a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelAboveTest.java b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelAboveTest.java index 0e8ef8cb344d3..b96233e999946 100644 --- a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelAboveTest.java +++ b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelAboveTest.java @@ -13,7 +13,7 @@ * and only messages at same runtime log level or above are shown. */ @QuarkusTest -@QuarkusTestResource(SetRuntimeLogLevels.class) +@QuarkusTestResource(SetCategoryRuntimeLogLevels.class) public class LoggingMinLevelAboveTest { @Test diff --git a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelByDefaultTest.java b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelByDefaultTest.java index 423be57b74ef0..d52fce3a7b56f 100644 --- a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelByDefaultTest.java +++ b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelByDefaultTest.java @@ -13,7 +13,7 @@ * and there's no applicable runtime log level override. */ @QuarkusTest -@QuarkusTestResource(SetRuntimeLogLevels.class) +@QuarkusTestResource(SetCategoryRuntimeLogLevels.class) public class LoggingMinLevelByDefaultTest { @Test diff --git a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelGlobalTest.java b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelGlobalTest.java new file mode 100644 index 0000000000000..a94ba4de4fb12 --- /dev/null +++ b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelGlobalTest.java @@ -0,0 +1,46 @@ +package io.quarkus.it.logging.minlevel.unset; + +import static io.restassured.RestAssured.given; +import static org.hamcrest.CoreMatchers.is; + +import org.junit.jupiter.api.Test; + +import io.quarkus.test.common.QuarkusTestResource; +import io.quarkus.test.junit.QuarkusTest; + +/** + * Tests that logging works as expected when min-level is default, + * and the global log level is below it. + */ +@QuarkusTest +@QuarkusTestResource(SetGlobalRuntimeLogLevel.class) +public class LoggingMinLevelGlobalTest { + + @Test + public void testInfo() { + given() + .when().get("/log/bydefault/info") + .then() + .statusCode(200) + .body(is("true")); + } + + @Test + public void testWarn() { + given() + .when().get("/log/bydefault/warn") + .then() + .statusCode(200) + .body(is("true")); + } + + @Test + public void testNotTrace() { + given() + .when().get("/log/bydefault/not-trace") + .then() + .statusCode(200) + .body(is("true")); + } + +} diff --git a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelPromoteTest.java b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelPromoteTest.java index 8dfdb29d39301..3afd9a5c02b6a 100644 --- a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelPromoteTest.java +++ b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/LoggingMinLevelPromoteTest.java @@ -16,7 +16,7 @@ * it will be automatically promoted to DEBUG. */ @QuarkusTest -@QuarkusTestResource(SetRuntimeLogLevels.class) +@QuarkusTestResource(SetCategoryRuntimeLogLevels.class) public class LoggingMinLevelPromoteTest { @Test diff --git a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetRuntimeLogLevels.java b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetCategoryRuntimeLogLevels.java similarity index 86% rename from integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetRuntimeLogLevels.java rename to integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetCategoryRuntimeLogLevels.java index 777bb6e0db2fc..3789ac4addeac 100644 --- a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetRuntimeLogLevels.java +++ b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetCategoryRuntimeLogLevels.java @@ -5,7 +5,7 @@ import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; -public final class SetRuntimeLogLevels implements QuarkusTestResourceLifecycleManager { +public final class SetCategoryRuntimeLogLevels implements QuarkusTestResourceLifecycleManager { @Override public Map start() { diff --git a/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetGlobalRuntimeLogLevel.java b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetGlobalRuntimeLogLevel.java new file mode 100644 index 0000000000000..ba3f845a542c5 --- /dev/null +++ b/integration-tests/logging-min-level-unset/src/test/java/io/quarkus/it/logging/minlevel/unset/SetGlobalRuntimeLogLevel.java @@ -0,0 +1,21 @@ +package io.quarkus.it.logging.minlevel.unset; + +import java.util.HashMap; +import java.util.Map; + +import io.quarkus.test.common.QuarkusTestResourceLifecycleManager; + +public final class SetGlobalRuntimeLogLevel implements QuarkusTestResourceLifecycleManager { + + @Override + public Map start() { + final Map systemProperties = new HashMap<>(); + systemProperties.put("quarkus.log.level", "TRACE"); + return systemProperties; + } + + @Override + public void stop() { + } + +}