Skip to content

Commit

Permalink
Stop SpringEnvironmentPropertySource on Log4j shutdown
Browse files Browse the repository at this point in the history
  • Loading branch information
mhalbritter committed Nov 16, 2023
1 parent 0a5c21f commit c52ad97
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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 <a href="https://logging.apache.org/log4j/2.x/">Log4j 2</a>.
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand All @@ -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;
}
}

0 comments on commit c52ad97

Please sign in to comment.