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
This commit is contained in:
Ralph Goers 2022-10-12 11:17:09 -07:00 committed by Phillip Webb
parent 65109bda10
commit 05a2bd4585
1 changed files with 8 additions and 0 deletions

View File

@ -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<Level> 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);