From 6da89e72ef8827357b6cbc41d2020e9817dec8e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Lu=C3=ADs=20Duarte?= Date: Wed, 26 Oct 2022 18:56:21 +0100 Subject: [PATCH] Ensure Logback is refreshed before application starts --- .../io/micronaut/logging/LoggingSystem.java | 7 +++++++ .../PropertiesLoggingLevelsConfigurer.java | 10 ++++++--- .../logging/impl/LogbackLoggingSystem.java | 5 +++++ .../LogbackLogLevelConfigurerSpec.groovy | 21 +++++++++++++++++++ 4 files changed, 40 insertions(+), 3 deletions(-) diff --git a/runtime/src/main/java/io/micronaut/logging/LoggingSystem.java b/runtime/src/main/java/io/micronaut/logging/LoggingSystem.java index ba0fbb4e1ea..29a30d82f72 100644 --- a/runtime/src/main/java/io/micronaut/logging/LoggingSystem.java +++ b/runtime/src/main/java/io/micronaut/logging/LoggingSystem.java @@ -38,4 +38,11 @@ public interface LoggingSystem { */ void setLogLevel(@NotBlank String name, @NotNull LogLevel level); + /** + * Refreshes Logging System with the goal of cleaning its internal caches + * + */ + default void refresh() { + + } } diff --git a/runtime/src/main/java/io/micronaut/logging/PropertiesLoggingLevelsConfigurer.java b/runtime/src/main/java/io/micronaut/logging/PropertiesLoggingLevelsConfigurer.java index 729903b807a..ab4f4401d67 100644 --- a/runtime/src/main/java/io/micronaut/logging/PropertiesLoggingLevelsConfigurer.java +++ b/runtime/src/main/java/io/micronaut/logging/PropertiesLoggingLevelsConfigurer.java @@ -64,6 +64,7 @@ final class PropertiesLoggingLevelsConfigurer implements ApplicationEventListene public PropertiesLoggingLevelsConfigurer(Environment environment, List loggingSystems) { this.environment = environment; this.loggingSystems = loggingSystems; + initLogging(); configureLogLevels(); } @@ -74,9 +75,12 @@ public PropertiesLoggingLevelsConfigurer(Environment environment, List key.startsWith(LOGGER_LEVELS_PROPERTY_PREFIX))) { - configureLogLevels(); - } + initLogging(); + configureLogLevels(); + } + + private void initLogging() { + this.loggingSystems.forEach(LoggingSystem::refresh); } private void configureLogLevels() { diff --git a/runtime/src/main/java/io/micronaut/logging/impl/LogbackLoggingSystem.java b/runtime/src/main/java/io/micronaut/logging/impl/LogbackLoggingSystem.java index 9a7cf719bf3..8ac87e3223c 100644 --- a/runtime/src/main/java/io/micronaut/logging/impl/LogbackLoggingSystem.java +++ b/runtime/src/main/java/io/micronaut/logging/impl/LogbackLoggingSystem.java @@ -40,6 +40,11 @@ public void setLogLevel(String name, LogLevel level) { getLoggerContext().getLogger(name).setLevel(toLevel(level)); } + @Override + public void refresh() { + getLoggerContext().reset(); + } + /** * @return The logback {@link LoggerContext} */ diff --git a/runtime/src/test/groovy/io/micronaut/logging/LogbackLogLevelConfigurerSpec.groovy b/runtime/src/test/groovy/io/micronaut/logging/LogbackLogLevelConfigurerSpec.groovy index e836f362897..4f81adc8057 100644 --- a/runtime/src/test/groovy/io/micronaut/logging/LogbackLogLevelConfigurerSpec.groovy +++ b/runtime/src/test/groovy/io/micronaut/logging/LogbackLogLevelConfigurerSpec.groovy @@ -142,4 +142,25 @@ logger: 'foo.bar3' | Level.INFO } + void 'logging refresh is properly called on application start'() { + given: + ApplicationContext context = ApplicationContext.builder() + .build() + + def loggingSystem = context.getBean(LoggingSystem.class) + def spy = Spy(loggingSystem) + + when: + SystemLambda.withEnvironmentVariable("LOGGER_LEVELS_FOO_BAR3", "INFO") + .execute(() -> { + context.start() + }) + + then: + 1 * spy.refresh() + + cleanup: + context.close() + } + }