Switch ConfigFileAppListener to use DeferredLog

Update ConfigFileApplicationListener to use the recently introduced
DeferredLog class rather than storing its own log messages.
This commit is contained in:
Phillip Webb 2015-07-06 13:14:33 -07:00
parent 0cf6efca4f
commit 155f04ef30
2 changed files with 26 additions and 56 deletions

View File

@ -28,7 +28,6 @@ import java.util.Queue;
import java.util.Set; import java.util.Set;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException; import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.BeanFactoryPostProcessor; import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; 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.context.event.ApplicationPreparedEvent;
import org.springframework.boot.env.EnumerableCompositePropertySource; import org.springframework.boot.env.EnumerableCompositePropertySource;
import org.springframework.boot.env.PropertySourcesLoader; import org.springframework.boot.env.PropertySourcesLoader;
import org.springframework.boot.logging.DeferredLog;
import org.springframework.context.ApplicationEvent; import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener; import org.springframework.context.ApplicationListener;
import org.springframework.context.ConfigurableApplicationContext; import org.springframework.context.ConfigurableApplicationContext;
@ -93,8 +93,6 @@ import org.springframework.validation.BindException;
public class ConfigFileApplicationListener implements public class ConfigFileApplicationListener implements
ApplicationListener<ApplicationEvent>, Ordered { ApplicationListener<ApplicationEvent>, Ordered {
private static Log logger = LogFactory.getLog(ConfigFileApplicationListener.class);
private static final String DEFAULT_PROPERTIES = "defaultProperties"; private static final String DEFAULT_PROPERTIES = "defaultProperties";
// Note the order is from least to most specific (last one wins) // 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; public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
private final DeferredLog logger = new DeferredLog();
private String searchLocations; private String searchLocations;
private String names; private String names;
@ -120,8 +120,6 @@ public class ConfigFileApplicationListener implements
private final ConversionService conversionService = new DefaultConversionService(); private final ConversionService conversionService = new DefaultConversionService();
private final List<LogMessage> logMessages = new ArrayList<LogMessage>();
@Override @Override
public void onApplicationEvent(ApplicationEvent event) { public void onApplicationEvent(ApplicationEvent event) {
if (event instanceof ApplicationEnvironmentPreparedEvent) { if (event instanceof ApplicationEnvironmentPreparedEvent) {
@ -148,17 +146,10 @@ public class ConfigFileApplicationListener implements
} }
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) { private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
logMessages();
addPostProcessors(event.getApplicationContext());
}
private void logMessages() {
// logging is deferred because the Logging initialization might not have // logging is deferred because the Logging initialization might not have
// run at the time that config file decisions are taken // run at the time that config file decisions are taken
for (LogMessage logMessage : this.logMessages) { this.logger.replayTo(ConfigFileApplicationListener.class);
logMessage.log(logger); addPostProcessors(event.getApplicationContext());
}
this.logMessages.clear();
} }
/** /**
@ -281,6 +272,8 @@ public class ConfigFileApplicationListener implements
*/ */
private class Loader { private class Loader {
private final Log logger = ConfigFileApplicationListener.this.logger;
private final ConfigurableEnvironment environment; private final ConfigurableEnvironment environment;
private final ResourceLoader resourceLoader; private final ResourceLoader resourceLoader;
@ -291,8 +284,6 @@ public class ConfigFileApplicationListener implements
private boolean activatedProfiles; private boolean activatedProfiles;
private final List<LogMessage> logMessages = ConfigFileApplicationListener.this.logMessages;
public Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) { public Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
this.environment = environment; this.environment = environment;
this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader() this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader()
@ -410,9 +401,10 @@ public class ConfigFileApplicationListener implements
} }
if (resource == null || !resource.exists()) { if (resource == null || !resource.exists()) {
msg.append(" resource not found"); msg.append(" resource not found");
this.logMessages.add(LogMessage.trace(msg)); this.logger.trace(msg);
} else { }
this.logMessages.add(LogMessage.debug(msg)); else {
this.logger.debug(msg);
} }
return propertySource; return propertySource;
} }
@ -420,8 +412,8 @@ public class ConfigFileApplicationListener implements
private void maybeActivateProfiles(Object value) { private void maybeActivateProfiles(Object value) {
if (this.activatedProfiles) { if (this.activatedProfiles) {
if (value != null) { if (value != null) {
this.logMessages.add(LogMessage.debug("Profiles already activated, '" + value this.logger.debug("Profiles already activated, '" + value
+ "' will not be applied")); + "' will not be applied");
} }
return; return;
} }
@ -429,8 +421,8 @@ public class ConfigFileApplicationListener implements
Set<String> profiles = getProfilesForValue(value); Set<String> profiles = getProfilesForValue(value);
activateProfiles(profiles); activateProfiles(profiles);
if (profiles.size() > 0) { if (profiles.size() > 0) {
this.logMessages.add(LogMessage.debug("Activated profiles " this.logger.debug("Activated profiles "
+ StringUtils.collectionToCommaDelimitedString(profiles))); + StringUtils.collectionToCommaDelimitedString(profiles));
this.activatedProfiles = true; 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);
}
}
}
} }

View File

@ -20,6 +20,7 @@ import java.util.ArrayList;
import java.util.List; import java.util.List;
import org.apache.commons.logging.Log; 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 * 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)); 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) { for (Line line : this.lines) {
line.replayTo(log); line.replayTo(destination);
} }
this.lines.clear(); 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) { public static Log replay(Log source, Log destination) {
if (source instanceof DeferredLog) { if (source instanceof DeferredLog) {
((DeferredLog) source).replayTo(destination); ((DeferredLog) source).replayTo(destination);