From 6d3a60b64c45ac864a03e9f9f343ef9a0fad4185 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Fri, 30 Jan 2015 11:39:55 +0000 Subject: [PATCH] Add a call to update log levels during bootstrap Since the bootstrap customizations are applied in ApplicationContextInitializers they come too late to affect the logging levels. But the LoggingRebinder listener isn't installed yet, and the context is not ready for events to be published. So to get the logging changes in as early as possible we actually need to explicitly apply them in the property source initializer as soon as the remote properties are available. Fixes gh-74, fixes gh-75 --- .../PropertySourceBootstrapConfiguration.java | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java index b79a2023..18fac7b7 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java @@ -18,6 +18,7 @@ package org.springframework.cloud.bootstrap.config; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import java.util.List; import org.apache.commons.logging.Log; @@ -31,6 +32,8 @@ import org.springframework.cloud.bootstrap.BootstrapApplicationListener; import org.springframework.cloud.config.client.ConfigClientProperties; import org.springframework.cloud.config.client.ConfigServicePropertySourceLocator; import org.springframework.cloud.config.client.PropertySourceLocator; +import org.springframework.cloud.context.environment.EnvironmentChangeEvent; +import org.springframework.cloud.logging.LoggingRebinder; import org.springframework.context.ApplicationContextInitializer; import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.annotation.Bean; @@ -90,16 +93,26 @@ public class PropertySourceBootstrapConfiguration implements propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME); } insertPropertySources(propertySources, composite); + setLogLevels(applicationContext.getEnvironment()); } } + private void setLogLevels(ConfigurableEnvironment environment) { + LoggingRebinder rebinder = new LoggingRebinder(); + rebinder.setEnvironment(environment); + // We can't fire the event in the ApplicationContext here (too early), but we can + // create our own listener and poke it (it doesn't need the key changes) + rebinder.onApplicationEvent(new EnvironmentChangeEvent(Collections + . emptySet())); + } + private void insertPropertySources(MutablePropertySources propertySources, CompositePropertySource composite) { MutablePropertySources incoming = new MutablePropertySources(); incoming.addFirst(composite); PropertySourceBootstrapProperties remoteProperties = new PropertySourceBootstrapProperties(); - new RelaxedDataBinder(remoteProperties, "spring.cloud.config").bind(new PropertySourcesPropertyValues( - incoming)); + new RelaxedDataBinder(remoteProperties, "spring.cloud.config") + .bind(new PropertySourcesPropertyValues(incoming)); if (!remoteProperties.isAllowOverride() || remoteProperties.isOverrideSystemProperties()) { propertySources.addFirst(composite);