From d665441ca955418d2456408d5483144f35ef6825 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Wed, 12 Oct 2022 11:17:09 -0700 Subject: [PATCH] Polish 'Add Spring Environment to LoggerContext' See gh-32731 --- .../logging/log4j2/Log4J2LoggingSystem.java | 18 ++++++++++++++---- .../log4j2/Log4J2LoggingSystemTests.java | 10 ++++++++++ 2 files changed, 24 insertions(+), 4 deletions(-) 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 d36a6d0f439..b1ec7c67a13 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,6 +58,7 @@ 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; @@ -85,10 +86,8 @@ public class Log4J2LoggingSystem extends AbstractLoggingSystem { private static final String LOG4J_LOG_MANAGER = "org.apache.logging.log4j.jul.LogManager"; - /** - * Identifies the Spring environment. - */ - public static final String ENVIRONMENT_KEY = "SpringEnvironment"; + static final String ENVIRONMENT_KEY = Conventions.getQualifiedAttributeName(Log4J2LoggingSystem.class, + "environment"); private static final LogLevels LEVELS = new LogLevels<>(); @@ -475,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 6045e50a3f3..3155bf13f84 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('.', '/');