diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java index a1a9747c43..688037d8ec 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystem.java @@ -148,7 +148,7 @@ public class LogbackLoggingSystem extends Slf4JLoggingSystem { } Environment environment = initializationContext.getEnvironment(); // Apply system properties directly in case the same JVM runs multiple apps - new LoggingSystemProperties(environment, context::putProperty).apply(logFile); + new LogbackLoggingSystemProperties(environment, context::putProperty).apply(logFile); LogbackConfigurator configurator = debug ? new DebugLogbackConfigurator(context) : new LogbackConfigurator(context); new DefaultLogbackConfiguration(initializationContext, logFile).apply(configurator); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java index 9982bcfb62..0586d1e3e9 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/logback/LogbackLoggingSystemProperties.java @@ -17,6 +17,7 @@ package org.springframework.boot.logging.logback; import java.nio.charset.Charset; +import java.util.function.BiConsumer; import ch.qos.logback.core.util.FileSize; @@ -66,6 +67,16 @@ public class LogbackLoggingSystemProperties extends LoggingSystemProperties { super(environment); } + /** + * Create a new {@link LogbackLoggingSystemProperties} instance. + * @param environment the source environment + * @param setter setter used to apply the property + * @since 2.4.3 + */ + public LogbackLoggingSystemProperties(Environment environment, BiConsumer setter) { + super(environment, setter); + } + @Override protected Charset getDefaultCharset() { return Charset.defaultCharset(); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java index 0788dcdaa9..3b8fc3a13e 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/logback/LogbackLoggingSystemTests.java @@ -17,11 +17,15 @@ package org.springframework.boot.logging.logback; import java.io.File; +import java.lang.reflect.Field; +import java.lang.reflect.Modifier; +import java.nio.charset.Charset; import java.nio.charset.StandardCharsets; import java.util.Arrays; import java.util.EnumSet; import java.util.HashSet; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.logging.Handler; import java.util.logging.LogManager; @@ -57,6 +61,7 @@ import org.springframework.core.convert.support.ConfigurableConversionService; import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.mock.env.MockEnvironment; import org.springframework.test.util.ReflectionTestUtils; +import org.springframework.util.ReflectionUtils; import org.springframework.util.StringUtils; import static org.assertj.core.api.Assertions.assertThat; @@ -501,6 +506,30 @@ class LogbackLoggingSystemTests extends AbstractLoggingSystemTests { assertThat(System.getProperty(LoggingSystemProperties.LOG_FILE)).endsWith("example.log"); } + @Test + void initializeShouldApplyLogbackSystemPropertiesToTheContext() { + this.environment.setProperty("logging.logback.rollingpolicy.file-name-pattern", "file-name-pattern"); + this.environment.setProperty("logging.logback.rollingpolicy.clean-history-on-start", "true"); + this.environment.setProperty("logging.logback.rollingpolicy.max-file-size", "10MB"); + this.environment.setProperty("logging.logback.rollingpolicy.total-size-cap", "100MB"); + this.environment.setProperty("logging.logback.rollingpolicy.max-history", "20"); + this.loggingSystem.beforeInitialize(); + initialize(this.initializationContext, null, null); + LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory(); + Map properties = loggerContext.getCopyOfPropertyMap(); + Set expectedProperties = new HashSet(); + ReflectionUtils.doWithFields(LogbackLoggingSystemProperties.class, + (field) -> expectedProperties.add((String) field.get(null)), this::isPublicStaticFinal); + expectedProperties.removeAll(Arrays.asList("LOG_FILE", "LOG_PATH")); + assertThat(properties).containsOnlyKeys(expectedProperties); + assertThat(properties).containsEntry("CONSOLE_LOG_CHARSET", Charset.defaultCharset().name()); + } + + private boolean isPublicStaticFinal(Field field) { + int modifiers = field.getModifiers(); + return Modifier.isPublic(modifiers) && Modifier.isStatic(modifiers) && Modifier.isFinal(modifiers); + } + @Test void initializationIsOnlyPerformedOnceUntilCleanedUp() { LoggerContext loggerContext = (LoggerContext) StaticLoggerBinder.getSingleton().getLoggerFactory();