Adjust the logging lifecycle during bootstrap
Anticipating a change in Spring Boot 1.4.1 to support child context creation without (needless) re-initialization of the logging system, we need to carefully manage the lifecycle, in particular calling cleanUp() when we know there are changes in the pipeline. Fixes gh-125
This commit is contained in:
@@ -29,6 +29,7 @@ import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.builder.ParentContextApplicationContextInitializer;
|
||||
import org.springframework.boot.builder.SpringApplicationBuilder;
|
||||
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
|
||||
import org.springframework.boot.logging.LoggingSystem;
|
||||
import org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer;
|
||||
import org.springframework.context.ApplicationContextInitializer;
|
||||
import org.springframework.context.ApplicationListener;
|
||||
@@ -84,6 +85,11 @@ public class BootstrapApplicationListener
|
||||
ConfigurableApplicationContext context = bootstrapServiceContext(environment,
|
||||
event.getSpringApplication());
|
||||
apply(context, event.getSpringApplication(), environment);
|
||||
// Clean up the logging system. Logging will go dark until the
|
||||
// ConfigFileApplicationListener fires, but this is the price we pay for that
|
||||
// listener being able to adjust the log levels according to what it finds in its
|
||||
// own configuration.
|
||||
LoggingSystem.get(ClassUtils.getDefaultClassLoader()).cleanUp();
|
||||
}
|
||||
|
||||
private ConfigurableApplicationContext bootstrapServiceContext(
|
||||
@@ -121,9 +127,7 @@ public class BootstrapApplicationListener
|
||||
.profiles(environment.getActiveProfiles()).bannerMode(Mode.OFF)
|
||||
.environment(bootstrapEnvironment)
|
||||
.properties("spring.application.name:" + configName)
|
||||
.registerShutdownHook(false)
|
||||
.logStartupInfo(false)
|
||||
.web(false);
|
||||
.registerShutdownHook(false).logStartupInfo(false).web(false);
|
||||
List<Class<?>> sources = new ArrayList<>();
|
||||
for (String name : names) {
|
||||
Class<?> cls = ClassUtils.resolveClassName(name, null);
|
||||
|
||||
@@ -118,6 +118,11 @@ public class PropertySourceBootstrapConfiguration implements
|
||||
.get(LoggingSystem.class.getClassLoader());
|
||||
try {
|
||||
ResourceUtils.getURL(logConfig).openStream().close();
|
||||
// Three step initialization that accounts for the clean up of the logging
|
||||
// context before initialization. Spring Boot doesn't initialize a logging
|
||||
// system that hasn't had this sequence applied (since 1.4.1).
|
||||
system.cleanUp();
|
||||
system.beforeInitialize();
|
||||
system.initialize(new LoggingInitializationContext(environment),
|
||||
logConfig, logFile);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user