diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java index f7aad8dee2..b1ec7c67a1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java @@ -58,8 +58,10 @@ import org.springframework.boot.logging.LoggerConfiguration; import org.springframework.boot.logging.LoggingInitializationContext; import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystemFactory; +import org.springframework.core.Conventions; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; +import org.springframework.core.env.Environment; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; @@ -84,6 +86,9 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { private static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager"; + static final String ENVIRONMENT_KEY = Conventions.getQualifiedAttributeName(Log4J2LoggingSystem.class, + "environment"); + private static final LogLevels LEVELS = new LogLevels<>(); static { @@ -227,6 +232,8 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { if (isAlreadyInitialized(loggerContext)) { return; } + Environment environment = initializationContext.getEnvironment(); + getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment); loggerContext.getConfiguration().removeFilter(FILTER); super.initialize(initializationContext, configLocation, logFile); markAsInitialized(loggerContext); @@ -467,6 +474,17 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { loggerContext.setExternalContext(null); } + /** + * Get the Spring {@link Environment} attached to the given {@link LoggerContext} or + * {@code null} if no environment is available. + * @param loggerContext the logger context + * @return the Spring {@link Environment} or {@code null} + * @since 3.0.0 + */ + public static Environment getEnvironment(LoggerContext loggerContext) { + return (Environment) ((loggerContext != null) ? loggerContext.getObject(ENVIRONMENT_KEY) : null); + } + /** * {@link LoggingSystemFactory} that returns {@link Log4J2LoggingSystem} if possible. */ diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java index 6045e50a3f..3155bf13f8 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystemTests.java @@ -57,6 +57,7 @@ import org.springframework.boot.testsupport.classpath.ClassPathExclusions; import org.springframework.boot.testsupport.logging.ConfigureClasspathToPreferLog4j2; import org.springframework.boot.testsupport.system.CapturedOutput; import org.springframework.boot.testsupport.system.OutputCaptureExtension; +import org.springframework.core.env.Environment; import org.springframework.mock.env.MockEnvironment; import org.springframework.util.ClassUtils; import org.springframework.util.StringUtils; @@ -438,6 +439,15 @@ class Log4J2LoggingSystemTests extends AbstractLoggingSystemTests { assertThat(this.loggingSystem.getConfiguration()).isInstanceOf(CompositeConfiguration.class); } + @Test + void initializeAttachesEnvironmentToLoggerContext() { + this.loggingSystem.beforeInitialize(); + this.loggingSystem.initialize(this.initializationContext, null, null); + LoggerContext loggerContext = (LoggerContext) LogManager.getContext(false); + Environment environment = Log4J2LoggingSystem.getEnvironment(loggerContext); + assertThat(environment).isSameAs(this.environment); + } + private String getRelativeClasspathLocation(String fileName) { String defaultPath = ClassUtils.getPackageName(getClass()); defaultPath = defaultPath.replace('.', '/');