Browse Source

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
pull/127/head
Dave Syer 8 years ago
parent
commit
7f687b10e3
  1. 10
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java
  2. 5
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java

10
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/BootstrapApplicationListener.java

@ -29,6 +29,7 @@ import org.springframework.boot.SpringApplication; @@ -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 @@ -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 @@ -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);

5
spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java

@ -118,6 +118,11 @@ public class PropertySourceBootstrapConfiguration implements @@ -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);
}

Loading…
Cancel
Save