Skip to content

Commit 7f687b1

Browse files
author
Dave Syer
committed
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 spring-projectsgh-125
1 parent cecac11 commit 7f687b1

File tree

2 files changed

+12
-3
lines changed

2 files changed

+12
-3
lines changed

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import org.springframework.boot.builder.ParentContextApplicationContextInitializer;
3030
import org.springframework.boot.builder.SpringApplicationBuilder;
3131
import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEvent;
32+
import org.springframework.boot.logging.LoggingSystem;
3233
import org.springframework.cloud.bootstrap.encrypt.EnvironmentDecryptApplicationInitializer;
3334
import org.springframework.context.ApplicationContextInitializer;
3435
import org.springframework.context.ApplicationListener;
@@ -84,6 +85,11 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) {
8485
ConfigurableApplicationContext context = bootstrapServiceContext(environment,
8586
event.getSpringApplication());
8687
apply(context, event.getSpringApplication(), environment);
88+
// Clean up the logging system. Logging will go dark until the
89+
// ConfigFileApplicationListener fires, but this is the price we pay for that
90+
// listener being able to adjust the log levels according to what it finds in its
91+
// own configuration.
92+
LoggingSystem.get(ClassUtils.getDefaultClassLoader()).cleanUp();
8793
}
8894

8995
private ConfigurableApplicationContext bootstrapServiceContext(
@@ -121,9 +127,7 @@ private ConfigurableApplicationContext bootstrapServiceContext(
121127
.profiles(environment.getActiveProfiles()).bannerMode(Mode.OFF)
122128
.environment(bootstrapEnvironment)
123129
.properties("spring.application.name:" + configName)
124-
.registerShutdownHook(false)
125-
.logStartupInfo(false)
126-
.web(false);
130+
.registerShutdownHook(false).logStartupInfo(false).web(false);
127131
List<Class<?>> sources = new ArrayList<>();
128132
for (String name : names) {
129133
Class<?> cls = ClassUtils.resolveClassName(name, null);

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ private void reinitializeLoggingSystem(ConfigurableEnvironment environment,
118118
.get(LoggingSystem.class.getClassLoader());
119119
try {
120120
ResourceUtils.getURL(logConfig).openStream().close();
121+
// Three step initialization that accounts for the clean up of the logging
122+
// context before initialization. Spring Boot doesn't initialize a logging
123+
// system that hasn't had this sequence applied (since 1.4.1).
124+
system.cleanUp();
125+
system.beforeInitialize();
121126
system.initialize(new LoggingInitializationContext(environment),
122127
logConfig, logFile);
123128
}

0 commit comments

Comments
 (0)