Skip to content

Commit c52ad97

Browse files
committed
Stop SpringEnvironmentPropertySource on Log4j shutdown
1 parent 0a5c21f commit c52ad97

File tree

2 files changed

+18
-6
lines changed

2 files changed

+18
-6
lines changed

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/Log4J2LoggingSystem.java

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,14 +66,12 @@
6666
import org.springframework.core.Conventions;
6767
import org.springframework.core.Ordered;
6868
import org.springframework.core.annotation.Order;
69-
import org.springframework.core.env.ConfigurableEnvironment;
7069
import org.springframework.core.env.Environment;
7170
import org.springframework.util.Assert;
7271
import org.springframework.util.ClassUtils;
7372
import org.springframework.util.CollectionUtils;
7473
import org.springframework.util.ResourceUtils;
7574
import org.springframework.util.StringUtils;
76-
import org.springframework.web.context.support.StandardServletEnvironment;
7775

7876
/**
7977
* {@link LoggingSystem} for <a href="https://logging.apache.org/log4j/2.x/">Log4j 2</a>.
@@ -132,6 +130,8 @@ public Result filter(Logger logger, Level level, Marker marker, String msg, Obje
132130

133131
};
134132

133+
private volatile SpringEnvironmentPropertySource springEnvironmentPropertySource;
134+
135135
public Log4J2LoggingSystem(ClassLoader classLoader) {
136136
super(classLoader);
137137
}
@@ -242,7 +242,8 @@ public void initialize(LoggingInitializationContext initializationContext, Strin
242242
Environment environment = initializationContext.getEnvironment();
243243
if (environment != null) {
244244
getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
245-
PropertiesUtil.getProperties().addPropertySource(new SpringEnvironmentPropertySource(environment));
245+
this.springEnvironmentPropertySource = new SpringEnvironmentPropertySource(environment);
246+
PropertiesUtil.getProperties().addPropertySource(this.springEnvironmentPropertySource);
246247
}
247248
loggerContext.getConfiguration().removeFilter(FILTER);
248249
super.initialize(initializationContext, configLocation, logFile);
@@ -452,9 +453,8 @@ private LevelConfiguration getLevelConfiguration(Level level) {
452453
@Override
453454
public Runnable getShutdownHandler() {
454455
return () -> {
455-
Environment environment = (Environment)getLoggerContext().getObject(ENVIRONMENT_KEY);
456-
if (environment instanceof ConfigurableEnvironment configurableEnvironment) {
457-
configurableEnvironment.getPropertySources().remove(StandardServletEnvironment.SERVLET_CONTEXT_PROPERTY_SOURCE_NAME);
456+
if (this.springEnvironmentPropertySource != null) {
457+
this.springEnvironmentPropertySource.stop();
458458
}
459459
getLoggerContext().stop();
460460
};

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/logging/log4j2/SpringEnvironmentPropertySource.java

+12
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,12 @@
2525
* Returns properties from Spring.
2626
*
2727
* @author Ralph Goers
28+
* @author Moritz Halbritter
2829
*/
2930
class SpringEnvironmentPropertySource implements PropertySource {
3031

32+
private volatile boolean stopped;
33+
3134
/**
3235
* System properties take precedence followed by properties in Log4j properties files.
3336
*/
@@ -47,12 +50,21 @@ public int getPriority() {
4750

4851
@Override
4952
public String getProperty(String key) {
53+
if (this.stopped) {
54+
return null;
55+
}
5056
return this.environment.getProperty(key);
5157
}
5258

5359
@Override
5460
public boolean containsProperty(String key) {
61+
if (this.stopped) {
62+
return false;
63+
}
5564
return this.environment.containsProperty(key);
5665
}
5766

67+
void stop() {
68+
this.stopped = true;
69+
}
5870
}

0 commit comments

Comments
 (0)