From 155f04ef30b9864fe58b93a8f433bcb4055b5040 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Mon, 6 Jul 2015 13:14:33 -0700 Subject: [PATCH] Switch ConfigFileAppListener to use DeferredLog Update ConfigFileApplicationListener to use the recently introduced DeferredLog class rather than storing its own log messages. --- .../config/ConfigFileApplicationListener.java | 69 ++++--------------- .../boot/logging/DeferredLog.java | 13 +++- 2 files changed, 26 insertions(+), 56 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java index 7f1460e1e6b..0b58d451925 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java @@ -28,7 +28,6 @@ import java.util.Queue; import java.util.Set; import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeansException; import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; @@ -38,6 +37,7 @@ import org.springframework.boot.context.event.ApplicationEnvironmentPreparedEven import org.springframework.boot.context.event.ApplicationPreparedEvent; import org.springframework.boot.env.EnumerableCompositePropertySource; import org.springframework.boot.env.PropertySourcesLoader; +import org.springframework.boot.logging.DeferredLog; import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationListener; import org.springframework.context.ConfigurableApplicationContext; @@ -93,8 +93,6 @@ import org.springframework.validation.BindException; public class ConfigFileApplicationListener implements ApplicationListener, Ordered { - private static Log logger = LogFactory.getLog(ConfigFileApplicationListener.class); - private static final String DEFAULT_PROPERTIES = "defaultProperties"; // Note the order is from least to most specific (last one wins) @@ -112,6 +110,8 @@ public class ConfigFileApplicationListener implements public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10; + private final DeferredLog logger = new DeferredLog(); + private String searchLocations; private String names; @@ -120,8 +120,6 @@ public class ConfigFileApplicationListener implements private final ConversionService conversionService = new DefaultConversionService(); - private final List logMessages = new ArrayList(); - @Override public void onApplicationEvent(ApplicationEvent event) { if (event instanceof ApplicationEnvironmentPreparedEvent) { @@ -148,17 +146,10 @@ public class ConfigFileApplicationListener implements } private void onApplicationPreparedEvent(ApplicationPreparedEvent event) { - logMessages(); - addPostProcessors(event.getApplicationContext()); - } - - private void logMessages() { // logging is deferred because the Logging initialization might not have // run at the time that config file decisions are taken - for (LogMessage logMessage : this.logMessages) { - logMessage.log(logger); - } - this.logMessages.clear(); + this.logger.replayTo(ConfigFileApplicationListener.class); + addPostProcessors(event.getApplicationContext()); } /** @@ -281,6 +272,8 @@ public class ConfigFileApplicationListener implements */ private class Loader { + private final Log logger = ConfigFileApplicationListener.this.logger; + private final ConfigurableEnvironment environment; private final ResourceLoader resourceLoader; @@ -291,8 +284,6 @@ public class ConfigFileApplicationListener implements private boolean activatedProfiles; - private final List logMessages = ConfigFileApplicationListener.this.logMessages; - public Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { this.environment = environment; this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader() @@ -410,9 +401,10 @@ public class ConfigFileApplicationListener implements } if (resource == null || !resource.exists()) { msg.append(" resource not found"); - this.logMessages.add(LogMessage.trace(msg)); - } else { - this.logMessages.add(LogMessage.debug(msg)); + this.logger.trace(msg); + } + else { + this.logger.debug(msg); } return propertySource; } @@ -420,8 +412,8 @@ public class ConfigFileApplicationListener implements private void maybeActivateProfiles(Object value) { if (this.activatedProfiles) { if (value != null) { - this.logMessages.add(LogMessage.debug("Profiles already activated, '" + value - + "' will not be applied")); + this.logger.debug("Profiles already activated, '" + value + + "' will not be applied"); } return; } @@ -429,8 +421,8 @@ public class ConfigFileApplicationListener implements Set profiles = getProfilesForValue(value); activateProfiles(profiles); if (profiles.size() > 0) { - this.logMessages.add(LogMessage.debug("Activated profiles " - + StringUtils.collectionToCommaDelimitedString(profiles))); + this.logger.debug("Activated profiles " + + StringUtils.collectionToCommaDelimitedString(profiles)); this.activatedProfiles = true; } } @@ -578,35 +570,4 @@ public class ConfigFileApplicationListener implements } - static class LogMessage { - private final String level; - - private final Object message; - - public LogMessage(String level, Object message) { - this.level = level; - this.message = message; - } - - public static LogMessage trace(Object message) { - return new LogMessage("trace", message); - } - - public static LogMessage debug(Object message) { - return new LogMessage("debug", message); - } - - public void log(Log logger) { - if (this.level.equals("trace")) { - logger.trace(this.message); - } - else if (this.level.equals("debug")) { - logger.debug(this.message); - } - else { - logger.info(this.message); - } - } - } - } diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/DeferredLog.java b/spring-boot/src/main/java/org/springframework/boot/logging/DeferredLog.java index b8b85ec8719..90c2381b623 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/DeferredLog.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/DeferredLog.java @@ -20,6 +20,7 @@ import java.util.ArrayList; import java.util.List; import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; /** * Deferred {@link Log} that can be used to store messages that shouldn't be written until @@ -126,13 +127,21 @@ public class DeferredLog implements Log { this.lines.add(new Line(level, message, t)); } - public void replayTo(Log log) { + public void replayTo(Class destination) { + replayTo(LogFactory.getLog(destination)); + } + + public void replayTo(Log destination) { for (Line line : this.lines) { - line.replayTo(log); + line.replayTo(destination); } this.lines.clear(); } + public static Log replay(Log source, Class destination) { + return replay(source, LogFactory.getLog(destination)); + } + public static Log replay(Log source, Log destination) { if (source instanceof DeferredLog) { ((DeferredLog) source).replayTo(destination);