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:
parent
0cf6efca4f
commit
155f04ef30
|
@ -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<ApplicationEvent>, 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<LogMessage> logMessages = new ArrayList<LogMessage>();
|
||||
|
||||
@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<LogMessage> 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<String> 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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
Loading…
Reference in New Issue