Skip to content

Commit 23ff7c9

Browse files
committed
Add debug logging to ConfigFileApplicationListener
Update ConfigFileApplicationListener to include more debug level output. Debug messages are recorded during onApplicationEnvironmentPreparedEvent but not actually output until onApplicationPreparedEvent. This is because the logging level might not have been correctly set until the context is completely prepared. Fixes gh-1584
1 parent 1422164 commit 23ff7c9

File tree

2 files changed

+54
-10
lines changed

2 files changed

+54
-10
lines changed

spring-boot/src/main/java/org/springframework/boot/context/config/ConfigFileApplicationListener.java

Lines changed: 46 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import java.util.Queue;
2828
import java.util.Set;
2929

30+
import org.apache.commons.logging.Log;
31+
import org.apache.commons.logging.LogFactory;
3032
import org.springframework.beans.BeansException;
3133
import org.springframework.beans.factory.config.BeanFactoryPostProcessor;
3234
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
@@ -106,6 +108,8 @@ public class ConfigFileApplicationListener implements
106108

107109
public static final int DEFAULT_ORDER = Ordered.HIGHEST_PRECEDENCE + 10;
108110

111+
private static Log logger = LogFactory.getLog(ConfigFileApplicationListener.class);
112+
109113
private String searchLocations;
110114

111115
private String names;
@@ -114,6 +118,8 @@ public class ConfigFileApplicationListener implements
114118

115119
private final ConversionService conversionService = new DefaultConversionService();
116120

121+
private final List<Object> debug = new ArrayList<Object>();
122+
117123
@Override
118124
public void onApplicationEvent(ApplicationEvent event) {
119125
if (event instanceof ApplicationEnvironmentPreparedEvent) {
@@ -140,9 +146,21 @@ private void onApplicationEnvironmentPreparedEvent(
140146
}
141147

142148
private void onApplicationPreparedEvent(ApplicationPreparedEvent event) {
149+
logDebugMessages();
143150
addPostProcessors(event.getApplicationContext());
144151
}
145152

153+
private void logDebugMessages() {
154+
// Debug logging is deferred because the Logging initialization might not have
155+
// run at the time that config file decisions are taken
156+
if (logger.isDebugEnabled()) {
157+
for (Object message : this.debug) {
158+
logger.debug(message);
159+
}
160+
}
161+
this.debug.clear();
162+
}
163+
146164
/**
147165
* Add config file property sources to the specified environment.
148166
* @param environment the environment to add source to
@@ -270,6 +288,8 @@ private class Loader {
270288

271289
private boolean activatedProfiles;
272290

291+
private final List<Object> debug = ConfigFileApplicationListener.this.debug;
292+
273293
public Loader(ConfigurableEnvironment environment, ResourceLoader resourceLoader) {
274294
this.environment = environment;
275295
this.resourceLoader = resourceLoader == null ? new DefaultResourceLoader()
@@ -280,7 +300,6 @@ public void load() throws IOException {
280300
this.propertiesLoader = new PropertySourcesLoader();
281301
this.profiles = Collections.asLifoQueue(new LinkedList<String>());
282302
this.activatedProfiles = false;
283-
284303
if (this.environment.containsProperty(ACTIVE_PROFILES_PROPERTY)) {
285304
// Any pre-existing active profiles set via property sources (e.g. System
286305
// properties) take precedence over those added in config files.
@@ -354,29 +373,46 @@ private void load(String location, String name, String profile)
354373
private PropertySource<?> loadIntoGroup(String identifier, String location,
355374
String profile) throws IOException {
356375
Resource resource = this.resourceLoader.getResource(location);
376+
PropertySource<?> propertySource = null;
357377
if (resource != null) {
358378
String name = "applicationConfig: [" + location + "]";
359379
String group = "applicationConfig: [" + identifier + "]";
360-
PropertySource<?> propertySource = this.propertiesLoader.load(resource,
361-
group, name, profile);
380+
propertySource = this.propertiesLoader.load(resource, group, name,
381+
profile);
362382
if (propertySource != null) {
363383
maybeActivateProfiles(propertySource
364384
.getProperty(ACTIVE_PROFILES_PROPERTY));
365385
addIncludeProfiles(propertySource
366386
.getProperty(INCLUDE_PROFILES_PROPERTY));
367387
}
368-
return propertySource;
369388
}
370-
return null;
389+
390+
StringBuilder msg = new StringBuilder();
391+
msg.append(propertySource == null ? "Skipped " : "Loaded ");
392+
msg.append("config file ");
393+
msg.append("'" + location + "' ");
394+
msg.append(StringUtils.hasLength(profile) ? "for profile " : "");
395+
msg.append(resource == null || !resource.exists() ? "resource not found" : "");
396+
this.debug.add(msg);
397+
398+
return propertySource;
371399
}
372400

373401
private void maybeActivateProfiles(Object value) {
374-
if (!this.activatedProfiles == true) {
375-
Set<String> profiles = getProfilesForValue(value);
376-
activateProfiles(profiles);
377-
if (profiles.size() > 0) {
378-
this.activatedProfiles = true;
402+
if (this.activatedProfiles) {
403+
if (value != null) {
404+
this.debug.add("Profiles already activated, '" + value
405+
+ "' will not be applied");
379406
}
407+
return;
408+
}
409+
410+
Set<String> profiles = getProfilesForValue(value);
411+
activateProfiles(profiles);
412+
if (profiles.size() > 0) {
413+
this.debug.add("Activated profiles "
414+
+ StringUtils.collectionToCommaDelimitedString(profiles));
415+
this.activatedProfiles = true;
380416
}
381417
}
382418

spring-boot/src/main/java/org/springframework/boot/context/config/RandomValuePropertySource.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818

1919
import java.util.Random;
2020

21+
import org.apache.commons.logging.Log;
22+
import org.apache.commons.logging.LogFactory;
2123
import org.springframework.core.env.ConfigurableEnvironment;
2224
import org.springframework.core.env.PropertySource;
2325
import org.springframework.core.env.StandardEnvironment;
@@ -33,6 +35,8 @@
3335
*/
3436
public class RandomValuePropertySource extends PropertySource<Random> {
3537

38+
private static Log logger = LogFactory.getLog(RandomValuePropertySource.class);
39+
3640
public RandomValuePropertySource(String name) {
3741
super(name, new Random());
3842
}
@@ -42,6 +46,9 @@ public Object getProperty(String name) {
4246
if (!name.startsWith("random.")) {
4347
return null;
4448
}
49+
if (logger.isTraceEnabled()) {
50+
logger.trace("Generating random property for '" + name + "'");
51+
}
4552
if (name.endsWith("int")) {
4653
return getSource().nextInt();
4754
}
@@ -71,6 +78,7 @@ public static void addToEnvironment(ConfigurableEnvironment environment) {
7178
environment.getPropertySources().addAfter(
7279
StandardEnvironment.SYSTEM_ENVIRONMENT_PROPERTY_SOURCE_NAME,
7380
new RandomValuePropertySource("random"));
81+
logger.trace("RandomValuePropertySource add to Environment");
7482
}
7583

7684
}

0 commit comments

Comments
 (0)