|
66 | 66 | import org.springframework.core.Conventions;
|
67 | 67 | import org.springframework.core.Ordered;
|
68 | 68 | import org.springframework.core.annotation.Order;
|
69 |
| -import org.springframework.core.env.ConfigurableEnvironment; |
70 | 69 | import org.springframework.core.env.Environment;
|
71 | 70 | import org.springframework.util.Assert;
|
72 | 71 | import org.springframework.util.ClassUtils;
|
73 | 72 | import org.springframework.util.CollectionUtils;
|
74 | 73 | import org.springframework.util.ResourceUtils;
|
75 | 74 | import org.springframework.util.StringUtils;
|
76 |
| -import org.springframework.web.context.support.StandardServletEnvironment; |
77 | 75 |
|
78 | 76 | /**
|
79 | 77 | * {@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
|
132 | 130 |
|
133 | 131 | };
|
134 | 132 |
|
| 133 | + private volatile SpringEnvironmentPropertySource springEnvironmentPropertySource; |
| 134 | + |
135 | 135 | public Log4J2LoggingSystem(ClassLoader classLoader) {
|
136 | 136 | super(classLoader);
|
137 | 137 | }
|
@@ -242,7 +242,8 @@ public void initialize(LoggingInitializationContext initializationContext, Strin
|
242 | 242 | Environment environment = initializationContext.getEnvironment();
|
243 | 243 | if (environment != null) {
|
244 | 244 | getLoggerContext().putObjectIfAbsent(ENVIRONMENT_KEY, environment);
|
245 |
| - PropertiesUtil.getProperties().addPropertySource(new SpringEnvironmentPropertySource(environment)); |
| 245 | + this.springEnvironmentPropertySource = new SpringEnvironmentPropertySource(environment); |
| 246 | + PropertiesUtil.getProperties().addPropertySource(this.springEnvironmentPropertySource); |
246 | 247 | }
|
247 | 248 | loggerContext.getConfiguration().removeFilter(FILTER);
|
248 | 249 | super.initialize(initializationContext, configLocation, logFile);
|
@@ -452,9 +453,8 @@ private LevelConfiguration getLevelConfiguration(Level level) {
|
452 | 453 | @Override
|
453 | 454 | public Runnable getShutdownHandler() {
|
454 | 455 | 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(); |
458 | 458 | }
|
459 | 459 | getLoggerContext().stop();
|
460 | 460 | };
|
|
0 commit comments