From c52ad976df6682e9788f9706ad73575699d1ac52 Mon Sep 17 00:00:00 2001 From: Moritz Halbritter Date: Thu, 16 Nov 2023 13:53:30 +0100 Subject: [PATCH] Stop SpringEnvironmentPropertySource on Log4j shutdown --- .../boot/logging/log4j2/Log4J2LoggingSystem.java | 12 ++++++------ .../log4j2/SpringEnvironmentPropertySource.java | 12 ++++++++++++ 2 files changed, 18 insertions(+), 6 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 3b98ee171b18..316937638776 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 @@ -66,14 +66,12 @@ import org.springframework.core.Conventions; import org.springframework.core.Ordered; import org.springframework.core.annotation.Order; -import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.Environment; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; import org.springframework.util.CollectionUtils; import org.springframework.util.ResourceUtils; import org.springframework.util.StringUtils; -import org.springframework.web.context.support.StandardServletEnvironment; /** * {@link LoggingSystem} for Log4j 2. @@ -132,6 +130,8 @@ public Result filter(Logger logger, Level level, Marker marker, String msg, Obje }; + private volatile SpringEnvironmentPropertySource springEnvironmentPropertySource; + public Log4J2LoggingSystem(ClassLoader classLoader) { super(classLoader); } @@ -242,7 +242,8 @@ public void initialize(LoggingInitializationContext initializationContext, Strin Environment environment = initializationContext.getEnvironment(); if (environment != null) { getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment); - PropertiesUtil.getProperties().addPropertySource(new SpringEnvironmentPropertySource(environment)); + this.springEnvironmentPropertySource = new SpringEnvironmentPropertySource(environment); + PropertiesUtil.getProperties().addPropertySource(this.springEnvironmentPropertySource); } loggerContext.getConfiguration().removeFilter(FILTER); super.initialize(initializationContext, configLocation, logFile); @@ -452,9 +453,8 @@ private LevelConfiguration getLevelConfiguration(Level level) { @Override public Runnable getShutdownHandler() { return () -> { - Environment environment = (Environment)getLoggerContext().getObject(ENVIRONMENT_KEY); - if (environment instanceof ConfigurableEnvironment configurableEnvironment) { - configurableEnvironment.getPropertySources().remove(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME); + if (this.springEnvironmentPropertySource != null) { + this.springEnvironmentPropertySource.stop(); } getLoggerContext().stop(); }; diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentPropertySource.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentPropertySource.java index 8b8671e536d3..e4c0f89b7e91 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentPropertySource.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentPropertySource.java @@ -25,9 +25,12 @@ * Returns properties from Spring. * * @author Ralph Goers + * @author Moritz Halbritter */ class SpringEnvironmentPropertySource implements PropertySource { + private volatile boolean stopped; + /** * System properties take precedence followed by properties in Log4j properties files. */ @@ -47,12 +50,21 @@ public int getPriority() { @Override public String getProperty(String key) { + if (this.stopped) { + return null; + } return this.environment.getProperty(key); } @Override public boolean containsProperty(String key) { + if (this.stopped) { + return false; + } return this.environment.containsProperty(key); } + void stop() { + this.stopped = true; + } }