Skip to content

Commit c6a0f1e

Browse files
committed
Polish logging for core.env package
1 parent 91f05c8 commit c6a0f1e

File tree

2 files changed

+45
-4
lines changed

2 files changed

+45
-4
lines changed

org.springframework.core/src/main/java/org/springframework/core/env/AbstractEnvironment.java

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -81,12 +81,15 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
8181
private Set<String> activeProfiles = new LinkedHashSet<String>();
8282
private Set<String> defaultProfiles = new LinkedHashSet<String>(this.getReservedDefaultProfiles());
8383

84-
private final MutablePropertySources propertySources = new MutablePropertySources();
84+
private final MutablePropertySources propertySources = new MutablePropertySources(logger);
8585
private final ConfigurablePropertyResolver propertyResolver = new PropertySourcesPropertyResolver(propertySources);
8686

8787

8888
public AbstractEnvironment() {
89+
String name = this.getClass().getSimpleName();
90+
logger.debug(String.format("Initializing new %s", name));
8991
this.customizePropertySources(propertySources);
92+
logger.debug(String.format("Initialized %s with PropertySources %s", name, propertySources));
9093
}
9194

9295
/**
@@ -187,7 +190,7 @@ public String[] getActiveProfiles() {
187190
*/
188191
protected Set<String> doGetActiveProfiles() {
189192
if (this.activeProfiles.isEmpty()) {
190-
String profiles = this.propertyResolver.getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
193+
String profiles = this.getProperty(ACTIVE_PROFILES_PROPERTY_NAME);
191194
if (StringUtils.hasText(profiles)) {
192195
setActiveProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles)));
193196
}
@@ -204,6 +207,7 @@ public void setActiveProfiles(String... profiles) {
204207
}
205208

206209
public void addActiveProfile(String profile) {
210+
logger.debug(String.format("Activating profile '%s'", profile));
207211
this.validateProfile(profile);
208212
this.activeProfiles.add(profile);
209213
}
@@ -226,7 +230,7 @@ public String[] getDefaultProfiles() {
226230
*/
227231
protected Set<String> doGetDefaultProfiles() {
228232
if (this.defaultProfiles.equals(this.getReservedDefaultProfiles())) {
229-
String profiles = this.propertyResolver.getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
233+
String profiles = this.getProperty(DEFAULT_PROFILES_PROPERTY_NAME);
230234
if (StringUtils.hasText(profiles)) {
231235
this.setDefaultProfiles(commaDelimitedListToStringArray(trimAllWhitespace(profiles)));
232236
}
@@ -413,7 +417,7 @@ public void setValueSeparator(String valueSeparator) {
413417

414418
@Override
415419
public String toString() {
416-
return format("%s [activeProfiles=%s, defaultProfiles=%s, propertySources=%s]",
420+
return format("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}",
417421
getClass().getSimpleName(), this.activeProfiles, this.defaultProfiles, this.propertySources);
418422
}
419423

org.springframework.core/src/main/java/org/springframework/core/env/MutablePropertySources.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,10 @@
1919
import java.util.Iterator;
2020
import java.util.LinkedList;
2121

22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
2224
import org.springframework.util.Assert;
25+
import org.springframework.util.StringUtils;
2326

2427
/**
2528
* Default implementation of the {@link PropertySources} interface.
@@ -39,24 +42,38 @@ public class MutablePropertySources implements PropertySources {
3942
static final String NON_EXISTENT_PROPERTY_SOURCE_MESSAGE = "PropertySource named [%s] does not exist";
4043
static final String ILLEGAL_RELATIVE_ADDITION_MESSAGE = "PropertySource named [%s] cannot be added relative to itself";
4144

45+
private final Log logger;
46+
4247
private final LinkedList<PropertySource<?>> propertySourceList = new LinkedList<PropertySource<?>>();
4348

49+
4450
/**
4551
* Create a new {@link MutablePropertySources} object.
4652
*/
4753
public MutablePropertySources() {
54+
this.logger = LogFactory.getLog(this.getClass());
4855
}
4956

5057
/**
5158
* Create a new {@code MutablePropertySources} from the given propertySources
5259
* object, preserving the original order of contained {@code PropertySource} objects.
5360
*/
5461
public MutablePropertySources(PropertySources propertySources) {
62+
this();
5563
for (PropertySource<?> propertySource : propertySources) {
5664
this.addLast(propertySource);
5765
}
5866
}
5967

68+
/**
69+
* Create a new {@link MutablePropertySources} object and inheriting the given logger,
70+
* usually from an enclosing {@link Environment}.
71+
*/
72+
MutablePropertySources(Log logger) {
73+
this.logger = logger;
74+
}
75+
76+
6077
public boolean contains(String name) {
6178
return this.propertySourceList.contains(PropertySource.named(name));
6279
}
@@ -73,6 +90,8 @@ public Iterator<PropertySource<?>> iterator() {
7390
* Add the given property source object with highest precedence.
7491
*/
7592
public void addFirst(PropertySource<?> propertySource) {
93+
logger.debug(String.format("Adding [%s] PropertySource with highest search precedence",
94+
propertySource.getName()));
7695
removeIfPresent(propertySource);
7796
this.propertySourceList.addFirst(propertySource);
7897
}
@@ -81,6 +100,8 @@ public void addFirst(PropertySource<?> propertySource) {
81100
* Add the given property source object with lowest precedence.
82101
*/
83102
public void addLast(PropertySource<?> propertySource) {
103+
logger.debug(String.format("Adding [%s] PropertySource with lowest search precedence",
104+
propertySource.getName()));
84105
removeIfPresent(propertySource);
85106
this.propertySourceList.addLast(propertySource);
86107
}
@@ -90,6 +111,8 @@ public void addLast(PropertySource<?> propertySource) {
90111
* than the named relative property source.
91112
*/
92113
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
114+
logger.debug(String.format("Adding [%s] PropertySource with search precedence immediately higher than [%s]",
115+
propertySource.getName(), relativePropertySourceName));
93116
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
94117
removeIfPresent(propertySource);
95118
int index = assertPresentAndGetIndex(relativePropertySourceName);
@@ -101,6 +124,8 @@ public void addBefore(String relativePropertySourceName, PropertySource<?> prope
101124
* than the named relative property source.
102125
*/
103126
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
127+
logger.debug(String.format("Adding [%s] PropertySource with search precedence immediately lower than [%s]",
128+
propertySource.getName(), relativePropertySourceName));
104129
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
105130
removeIfPresent(propertySource);
106131
int index = assertPresentAndGetIndex(relativePropertySourceName);
@@ -119,6 +144,7 @@ public int precedenceOf(PropertySource<?> propertySource) {
119144
* @param name the name of the property source to find and remove
120145
*/
121146
public PropertySource<?> remove(String name) {
147+
logger.debug(String.format("Removing [%s] PropertySource", name));
122148
int index = this.propertySourceList.indexOf(PropertySource.named(name));
123149
if (index >= 0) {
124150
return this.propertySourceList.remove(index);
@@ -134,6 +160,8 @@ public PropertySource<?> remove(String name) {
134160
* @see #contains
135161
*/
136162
public void replace(String name, PropertySource<?> propertySource) {
163+
logger.debug(String.format("Replacing [%s] PropertySource with [%s]",
164+
name, propertySource.getName()));
137165
int index = assertPresentAndGetIndex(name);
138166
this.propertySourceList.set(index, propertySource);
139167
}
@@ -145,6 +173,15 @@ public int size() {
145173
return this.propertySourceList.size();
146174
}
147175

176+
@Override
177+
public synchronized String toString() {
178+
String[] names = new String[this.size()];
179+
for (int i=0; i < size(); i++) {
180+
names[i] = this.propertySourceList.get(i).getName();
181+
}
182+
return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(names));
183+
}
184+
148185
/**
149186
* Ensure that the given property source is not being added relative to itself.
150187
*/

0 commit comments

Comments
 (0)