From ecaecb6d32e6ad4ee5d66019bf9bdc59819f4453 Mon Sep 17 00:00:00 2001 From: Dave Syer Date: Mon, 27 Apr 2015 17:41:04 +0100 Subject: [PATCH] Re-initialize logging system if the logging.config path changes Fixes https://github.com/spring-cloud/spring-cloud-config/issues/132 --- .../PropertySourceBootstrapConfiguration.java | 33 ++++++++++++++++--- .../cloud/logging/LoggingRebinder.java | 6 ++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java index 938b6b06..909988c7 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java @@ -27,6 +27,8 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.bind.PropertySourcesPropertyValues; import org.springframework.boot.bind.RelaxedDataBinder; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.boot.logging.LogFile; +import org.springframework.boot.logging.LoggingSystem; import org.springframework.cloud.bootstrap.BootstrapApplicationListener; import org.springframework.cloud.context.environment.EnvironmentChangeEvent; import org.springframework.cloud.logging.LoggingRebinder; @@ -39,6 +41,8 @@ import org.springframework.core.env.ConfigurableEnvironment; import org.springframework.core.env.MutablePropertySources; import org.springframework.core.env.PropertySource; import org.springframework.core.env.StandardEnvironment; +import org.springframework.util.ResourceUtils; +import org.springframework.util.StringUtils; /** * @author Dave Syer @@ -71,9 +75,10 @@ public class PropertySourceBootstrapConfiguration implements BOOTSTRAP_PROPERTY_SOURCE_NAME); AnnotationAwareOrderComparator.sort(this.propertySourceLocators); boolean empty = true; + ConfigurableEnvironment environment = applicationContext.getEnvironment(); for (PropertySourceLocator locator : this.propertySourceLocators) { PropertySource source = null; - source = locator.locate(applicationContext.getEnvironment()); + source = locator.locate(environment); if (source == null) { continue; } @@ -82,13 +87,33 @@ public class PropertySourceBootstrapConfiguration implements empty = false; } if (!empty) { - MutablePropertySources propertySources = applicationContext.getEnvironment() - .getPropertySources(); + MutablePropertySources propertySources = environment.getPropertySources(); + String logConfig = environment.resolvePlaceholders("${logging.config:}"); if (propertySources.contains(BOOTSTRAP_PROPERTY_SOURCE_NAME)) { propertySources.remove(BOOTSTRAP_PROPERTY_SOURCE_NAME); } insertPropertySources(propertySources, composite); - setLogLevels(applicationContext.getEnvironment()); + reinitializeLoggingSystem(environment, logConfig); + setLogLevels(environment); + } + } + + private void reinitializeLoggingSystem(ConfigurableEnvironment environment, + String oldLogConfig) { + String logConfig = environment.resolvePlaceholders("${logging.config:}"); + if (StringUtils.hasText(logConfig) && !logConfig.equals(oldLogConfig)) { + LoggingSystem system = LoggingSystem + .get(LoggingSystem.class.getClassLoader()); + try { + ResourceUtils.getURL(logConfig).openStream().close(); + LogFile logFile = LogFile.get(environment); + system.initialize(logConfig, logFile); + } + catch (Exception ex) { + PropertySourceBootstrapConfiguration.logger + .warn("Logging config file location '" + logConfig + + "' cannot be opened and will be ignored"); + } } } diff --git a/spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java b/spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java index b67cdb40..7b9663ca 100644 --- a/spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java +++ b/spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java @@ -31,7 +31,7 @@ import org.springframework.core.env.Environment; /** * Listener that looks for {@link EnvironmentChangeEvent} and rebinds logger levels if any * changed. - * + * * @author Dave Syer * */ @@ -49,11 +49,11 @@ public class LoggingRebinder implements ApplicationListener