Reduce ConfigFileApplicationListener logs
Previously, all attempts to log a configuration file were logged at DEBUG level which lead to a lot of noise as Spring Boot looks in many places by default. We now only log the files that are effectively found at DEBUG level and all failed attempts at TRACE level. Closes gh-3129
This commit is contained in:
parent
067c7a295c
commit
afd357f45a
|
@ -185,6 +185,9 @@ No matter what you set in the environment, Spring Boot will always load
|
||||||
`application.properties` as described above. If YAML is used then files with the '`.yml`'
|
`application.properties` as described above. If YAML is used then files with the '`.yml`'
|
||||||
extension are also added to the list by default.
|
extension are also added to the list by default.
|
||||||
|
|
||||||
|
Spring Boot logs the configuration files that are loaded at `DEBUG` level and the
|
||||||
|
candidates it has not found at `TRACE` level.
|
||||||
|
|
||||||
See {sc-spring-boot}/context/config/ConfigFileApplicationListener.{sc-ext}[`ConfigFileApplicationListener`]
|
See {sc-spring-boot}/context/config/ConfigFileApplicationListener.{sc-ext}[`ConfigFileApplicationListener`]
|
||||||
for more detail.
|
for more detail.
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,7 @@ import org.springframework.validation.BindException;
|
||||||
*
|
*
|
||||||
* @author Dave Syer
|
* @author Dave Syer
|
||||||
* @author Phillip Webb
|
* @author Phillip Webb
|
||||||
|
* @author Stephane Nicoll
|
||||||
*/
|
*/
|
||||||
public class ConfigFileApplicationListener implements
|
public class ConfigFileApplicationListener implements
|
||||||
ApplicationListener<ApplicationEvent>, Ordered {
|
ApplicationListener<ApplicationEvent>, Ordered {
|
||||||
|
@ -119,7 +120,7 @@ public class ConfigFileApplicationListener implements
|
||||||
|
|
||||||
private final ConversionService conversionService = new DefaultConversionService();
|
private final ConversionService conversionService = new DefaultConversionService();
|
||||||
|
|
||||||
private final List<Object> debug = new ArrayList<Object>();
|
private final List<LogMessage> logMessages = new ArrayList<LogMessage>();
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onApplicationEvent(ApplicationEvent event) {
|
public void onApplicationEvent(ApplicationEvent event) {
|
||||||
|
@ -147,19 +148,17 @@ public class ConfigFileApplicationListener implements
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
|
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
|
||||||
logDebugMessages();
|
logMessages();
|
||||||
addPostProcessors(event.getApplicationContext());
|
addPostProcessors(event.getApplicationContext());
|
||||||
}
|
}
|
||||||
|
|
||||||
private void logDebugMessages() {
|
private void logMessages() {
|
||||||
// Debug 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
|
||||||
if (logger.isDebugEnabled()) {
|
for (LogMessage logMessage : this.logMessages) {
|
||||||
for (Object message : this.debug) {
|
logMessage.log(logger);
|
||||||
logger.debug(message);
|
|
||||||
}
|
}
|
||||||
}
|
this.logMessages.clear();
|
||||||
this.debug.clear();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -292,7 +291,7 @@ public class ConfigFileApplicationListener implements
|
||||||
|
|
||||||
private boolean activatedProfiles;
|
private boolean activatedProfiles;
|
||||||
|
|
||||||
private final List<Object> debug = ConfigFileApplicationListener.this.debug;
|
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;
|
||||||
|
@ -411,16 +410,18 @@ 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));
|
||||||
|
} else {
|
||||||
|
this.logMessages.add(LogMessage.debug(msg));
|
||||||
}
|
}
|
||||||
this.debug.add(msg);
|
|
||||||
return propertySource;
|
return propertySource;
|
||||||
}
|
}
|
||||||
|
|
||||||
private void maybeActivateProfiles(Object value) {
|
private void maybeActivateProfiles(Object value) {
|
||||||
if (this.activatedProfiles) {
|
if (this.activatedProfiles) {
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
this.debug.add("Profiles already activated, '" + value
|
this.logMessages.add(LogMessage.debug("Profiles already activated, '" + value
|
||||||
+ "' will not be applied");
|
+ "' will not be applied"));
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -428,8 +429,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.debug.add("Activated profiles "
|
this.logMessages.add(LogMessage.debug("Activated profiles "
|
||||||
+ StringUtils.collectionToCommaDelimitedString(profiles));
|
+ StringUtils.collectionToCommaDelimitedString(profiles)));
|
||||||
this.activatedProfiles = true;
|
this.activatedProfiles = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -577,4 +578,35 @@ 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue