Browse Source

Re-initialize logging system if the logging.config path changes

Fixes https://github.com/spring-cloud/spring-cloud-config/issues/132
pull/30/head
Dave Syer 10 years ago
parent
commit
ecaecb6d32
  1. 33
      spring-cloud-context/src/main/java/org/springframework/cloud/bootstrap/config/PropertySourceBootstrapConfiguration.java
  2. 6
      spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java

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

@ -27,6 +27,8 @@ import org.springframework.beans.factory.annotation.Autowired; @@ -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; @@ -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 @@ -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 @@ -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");
}
}
}

6
spring-cloud-context/src/main/java/org/springframework/cloud/logging/LoggingRebinder.java

@ -31,7 +31,7 @@ import org.springframework.core.env.Environment; @@ -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<EnvironmentChangeEve @@ -49,11 +49,11 @@ public class LoggingRebinder implements ApplicationListener<EnvironmentChangeEve
@Override
public void onApplicationEvent(EnvironmentChangeEvent event) {
if (environment == null) {
if (this.environment == null) {
return;
}
LoggingSystem system = LoggingSystem.get(LoggingSystem.class.getClassLoader());
setLogLevels(system, environment);
setLogLevels(system, this.environment);
}
protected void setLogLevels(LoggingSystem system, Environment environment) {

Loading…
Cancel
Save