From 05a2bd458562cff76d0cbf4c27977c5fe59f9de7 Mon Sep 17 00:00:00 2001 From: Ralph Goers Date: Wed, 12 Oct 2022 11:17:09 -0700 Subject: [PATCH] Add Spring Environment to LoggerContext Update `Log4J2LoggingSystem` to add the Spring `Environment` to Log4j2's `LoggerContext`. This allow Log4j2 plugins to access the `Environment` if they need it. See gh-32731 --- .../boot/logging/log4j2/Log4J2LoggingSystem.java | 8 ++++++++ 1 file changed, 8 insertions(+) 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..d36a6d0f43 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 @@ -60,6 +60,7 @@ import org.springframework.boot.logging.LoggingSystem; import org.springframework.boot.logging.LoggingSystemFactory; 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 +85,11 @@ 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"; + private static final LogLevels LEVELS = new LogLevels<>(); static { @@ -227,6 +233,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);