Skip to content

Commit fac83b2

Browse files
committed
Consistent logging in Environment and PropertySource implementations
Issue: SPR-15825
1 parent 3cef5a1 commit fac83b2

8 files changed

+54
-71
lines changed

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

+11-15
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -121,9 +121,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
121121
*/
122122
public AbstractEnvironment() {
123123
customizePropertySources(this.propertySources);
124-
if (this.logger.isDebugEnabled()) {
125-
this.logger.debug(String.format(
126-
"Initialized %s with PropertySources %s", getClass().getSimpleName(), this.propertySources));
124+
if (logger.isDebugEnabled()) {
125+
logger.debug("Initialized " + getClass().getSimpleName() + " with PropertySources " + this.propertySources);
127126
}
128127
}
129128

@@ -262,8 +261,8 @@ public void setActiveProfiles(String... profiles) {
262261

263262
@Override
264263
public void addActiveProfile(String profile) {
265-
if (this.logger.isDebugEnabled()) {
266-
this.logger.debug(String.format("Activating profile '%s'", profile));
264+
if (logger.isDebugEnabled()) {
265+
logger.debug("Activating profile '" + profile + "'");
267266
}
268267
validateProfile(profile);
269268
doGetActiveProfiles();
@@ -393,9 +392,8 @@ protected String getSystemAttribute(String attributeName) {
393392
}
394393
catch (AccessControlException ex) {
395394
if (logger.isInfoEnabled()) {
396-
logger.info(String.format("Caught AccessControlException when accessing system " +
397-
"environment variable [%s]; its value will be returned [null]. Reason: %s",
398-
attributeName, ex.getMessage()));
395+
logger.info("Caught AccessControlException when accessing system environment variable '" +
396+
attributeName + "'; its value will be returned [null]. Reason: " + ex.getMessage());
399397
}
400398
return null;
401399
}
@@ -434,9 +432,8 @@ protected String getSystemAttribute(String attributeName) {
434432
}
435433
catch (AccessControlException ex) {
436434
if (logger.isInfoEnabled()) {
437-
logger.info(String.format("Caught AccessControlException when accessing system " +
438-
"property [%s]; its value will be returned [null]. Reason: %s",
439-
attributeName, ex.getMessage()));
435+
logger.info("Caught AccessControlException when accessing system property '" +
436+
attributeName + "'; its value will be returned [null]. Reason: " + ex.getMessage());
440437
}
441438
return null;
442439
}
@@ -569,9 +566,8 @@ public String resolveRequiredPlaceholders(String text) throws IllegalArgumentExc
569566

570567
@Override
571568
public String toString() {
572-
return String.format("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}",
573-
getClass().getSimpleName(), this.activeProfiles, this.defaultProfiles,
574-
this.propertySources);
569+
return getClass().getSimpleName() + " {activeProfiles=" + this.activeProfiles +
570+
", defaultProfiles=" + this.defaultProfiles + ", propertySources=" + this.propertySources + "}";
575571
}
576572

577573
}

spring-core/src/main/java/org/springframework/core/env/AbstractPropertyResolver.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public <T> T getProperty(String key, Class<T> targetType, T defaultValue) {
180180
public String getRequiredProperty(String key) throws IllegalStateException {
181181
String value = getProperty(key);
182182
if (value == null) {
183-
throw new IllegalStateException(String.format("required key [%s] not found", key));
183+
throw new IllegalStateException("Required key '" + key + "' not found");
184184
}
185185
return value;
186186
}
@@ -189,7 +189,7 @@ public String getRequiredProperty(String key) throws IllegalStateException {
189189
public <T> T getRequiredProperty(String key, Class<T> valueType) throws IllegalStateException {
190190
T value = getProperty(key, valueType);
191191
if (value == null) {
192-
throw new IllegalStateException(String.format("required key [%s] not found", key));
192+
throw new IllegalStateException("Required key '" + key + "' not found");
193193
}
194194
return value;
195195
}
@@ -233,7 +233,7 @@ private PropertyPlaceholderHelper createPlaceholderHelper(boolean ignoreUnresolv
233233
}
234234

235235
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
236-
return helper.replacePlaceholders(text, placeholderName -> getPropertyAsRawString(placeholderName));
236+
return helper.replacePlaceholders(text, this::getPropertyAsRawString);
237237
}
238238

239239
/**

spring-core/src/main/java/org/springframework/core/env/CompositePropertySource.java

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -119,8 +119,7 @@ public Collection<PropertySource<?>> getPropertySources() {
119119

120120
@Override
121121
public String toString() {
122-
return String.format("%s [name='%s', propertySources=%s]",
123-
getClass().getSimpleName(), this.name, this.propertySources);
122+
return getClass().getSimpleName() + " {name='" + this.name + "', propertySources=" + this.propertySources + "}";
124123
}
125124

126125
}

spring-core/src/main/java/org/springframework/core/env/MissingRequiredPropertiesException.java

+13-12
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -33,24 +33,25 @@ public class MissingRequiredPropertiesException extends IllegalStateException {
3333

3434
private final Set<String> missingRequiredProperties = new LinkedHashSet<>();
3535

36+
37+
void addMissingRequiredProperty(String key) {
38+
this.missingRequiredProperties.add(key);
39+
}
40+
41+
@Override
42+
public String getMessage() {
43+
return "The following properties were declared as required but could not be resolved: " +
44+
getMissingRequiredProperties();
45+
}
46+
3647
/**
3748
* Return the set of properties marked as required but not present
3849
* upon validation.
3950
* @see ConfigurablePropertyResolver#setRequiredProperties(String...)
4051
* @see ConfigurablePropertyResolver#validateRequiredProperties()
4152
*/
4253
public Set<String> getMissingRequiredProperties() {
43-
return missingRequiredProperties;
44-
}
45-
46-
void addMissingRequiredProperty(String key) {
47-
missingRequiredProperties.add(key);
54+
return this.missingRequiredProperties;
4855
}
4956

50-
@Override
51-
public String getMessage() {
52-
return String.format(
53-
"The following properties were declared as required but could " +
54-
"not be resolved: %s", this.getMissingRequiredProperties());
55-
}
5657
}

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

+13-22
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -24,7 +24,6 @@
2424
import org.apache.commons.logging.LogFactory;
2525

2626
import org.springframework.lang.Nullable;
27-
import org.springframework.util.StringUtils;
2827

2928
/**
3029
* Default implementation of the {@link PropertySources} interface.
@@ -95,8 +94,7 @@ public Iterator<PropertySource<?>> iterator() {
9594
*/
9695
public void addFirst(PropertySource<?> propertySource) {
9796
if (logger.isDebugEnabled()) {
98-
logger.debug(String.format("Adding [%s] PropertySource with highest search precedence",
99-
propertySource.getName()));
97+
logger.debug("Adding PropertySource '" + propertySource.getName() + "' with highest search precedence");
10098
}
10199
removeIfPresent(propertySource);
102100
this.propertySourceList.add(0, propertySource);
@@ -107,8 +105,7 @@ public void addFirst(PropertySource<?> propertySource) {
107105
*/
108106
public void addLast(PropertySource<?> propertySource) {
109107
if (logger.isDebugEnabled()) {
110-
logger.debug(String.format("Adding [%s] PropertySource with lowest search precedence",
111-
propertySource.getName()));
108+
logger.debug("Adding PropertySource '" + propertySource.getName() + "' with lowest search precedence");
112109
}
113110
removeIfPresent(propertySource);
114111
this.propertySourceList.add(propertySource);
@@ -120,8 +117,8 @@ public void addLast(PropertySource<?> propertySource) {
120117
*/
121118
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
122119
if (logger.isDebugEnabled()) {
123-
logger.debug(String.format("Adding [%s] PropertySource with search precedence immediately higher than [%s]",
124-
propertySource.getName(), relativePropertySourceName));
120+
logger.debug("Adding PropertySource '" + propertySource.getName() +
121+
"' with search precedence immediately higher than '" + relativePropertySourceName + "'");
125122
}
126123
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
127124
removeIfPresent(propertySource);
@@ -135,8 +132,8 @@ public void addBefore(String relativePropertySourceName, PropertySource<?> prope
135132
*/
136133
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
137134
if (logger.isDebugEnabled()) {
138-
logger.debug(String.format("Adding [%s] PropertySource with search precedence immediately lower than [%s]",
139-
propertySource.getName(), relativePropertySourceName));
135+
logger.debug("Adding PropertySource '" + propertySource.getName() +
136+
"' with search precedence immediately lower than '" + relativePropertySourceName + "'");
140137
}
141138
assertLegalRelativeAddition(relativePropertySourceName, propertySource);
142139
removeIfPresent(propertySource);
@@ -158,7 +155,7 @@ public int precedenceOf(PropertySource<?> propertySource) {
158155
@Nullable
159156
public PropertySource<?> remove(String name) {
160157
if (logger.isDebugEnabled()) {
161-
logger.debug(String.format("Removing [%s] PropertySource", name));
158+
logger.debug("Removing PropertySource '" + name + "'");
162159
}
163160
int index = this.propertySourceList.indexOf(PropertySource.named(name));
164161
return (index != -1 ? this.propertySourceList.remove(index) : null);
@@ -173,8 +170,7 @@ public PropertySource<?> remove(String name) {
173170
*/
174171
public void replace(String name, PropertySource<?> propertySource) {
175172
if (logger.isDebugEnabled()) {
176-
logger.debug(String.format("Replacing [%s] PropertySource with [%s]",
177-
name, propertySource.getName()));
173+
logger.debug("Replacing PropertySource '" + name + "' with '" + propertySource.getName() + "'");
178174
}
179175
int index = assertPresentAndGetIndex(name);
180176
this.propertySourceList.set(index, propertySource);
@@ -189,11 +185,7 @@ public int size() {
189185

190186
@Override
191187
public String toString() {
192-
String[] names = new String[this.size()];
193-
for (int i = 0; i < size(); i++) {
194-
names[i] = this.propertySourceList.get(i).getName();
195-
}
196-
return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(names));
188+
return this.propertySourceList.toString();
197189
}
198190

199191
/**
@@ -203,7 +195,7 @@ protected void assertLegalRelativeAddition(String relativePropertySourceName, Pr
203195
String newPropertySourceName = propertySource.getName();
204196
if (relativePropertySourceName.equals(newPropertySourceName)) {
205197
throw new IllegalArgumentException(
206-
String.format("PropertySource named [%s] cannot be added relative to itself", newPropertySourceName));
198+
"PropertySource named '" + newPropertySourceName + "' cannot be added relative to itself");
207199
}
208200
}
209201

@@ -224,14 +216,13 @@ private void addAtIndex(int index, PropertySource<?> propertySource) {
224216

225217
/**
226218
* Assert that the named property source is present and return its index.
227-
* @param name the {@linkplain PropertySource#getName() name of the property source}
228-
* to find
219+
* @param name {@linkplain PropertySource#getName() name of the property source} to find
229220
* @throws IllegalArgumentException if the named property source is not present
230221
*/
231222
private int assertPresentAndGetIndex(String name) {
232223
int index = this.propertySourceList.indexOf(PropertySource.named(name));
233224
if (index == -1) {
234-
throw new IllegalArgumentException(String.format("PropertySource named [%s] does not exist", name));
225+
throw new IllegalArgumentException("PropertySource named '" + name + "' does not exist");
235226
}
236227
return index;
237228
}

spring-core/src/main/java/org/springframework/core/env/PropertySource.java

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -157,11 +157,11 @@ public int hashCode() {
157157
@Override
158158
public String toString() {
159159
if (logger.isDebugEnabled()) {
160-
return String.format("%s@%s [name='%s', properties=%s]",
161-
getClass().getSimpleName(), System.identityHashCode(this), this.name, this.source);
160+
return getClass().getSimpleName() + "@" + System.identityHashCode(this) +
161+
" {name='" + this.name + "', properties=" + this.source + "}";
162162
}
163163
else {
164-
return String.format("%s [name='%s']", getClass().getSimpleName(), this.name);
164+
return getClass().getSimpleName() + " {name='" + this.name + "'}";
165165
}
166166
}
167167

@@ -242,11 +242,6 @@ public boolean containsProperty(String name) {
242242
public String getProperty(String name) {
243243
throw new UnsupportedOperationException(USAGE_ERROR);
244244
}
245-
246-
@Override
247-
public String toString() {
248-
return String.format("%s [name='%s']", getClass().getSimpleName(), this.name);
249-
}
250245
}
251246

252247
}

spring-core/src/main/java/org/springframework/core/env/PropertySourcesPropertyResolver.java

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2017 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -76,7 +76,8 @@ protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolv
7676
if (this.propertySources != null) {
7777
for (PropertySource<?> propertySource : this.propertySources) {
7878
if (logger.isTraceEnabled()) {
79-
logger.trace(String.format("Searching for key '%s' in [%s]", key, propertySource.getName()));
79+
logger.trace("Searching for key '" + key + "' in PropertySource '" +
80+
propertySource.getName() + "'");
8081
}
8182
Object value = propertySource.getProperty(key);
8283
if (value != null) {
@@ -89,7 +90,7 @@ protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolv
8990
}
9091
}
9192
if (logger.isDebugEnabled()) {
92-
logger.debug(String.format("Could not find key '%s' in any property source", key));
93+
logger.debug("Could not find key '" + key + "' in any property source");
9394
}
9495
return null;
9596
}
@@ -108,8 +109,8 @@ protected <T> T getProperty(String key, Class<T> targetValueType, boolean resolv
108109
*/
109110
protected void logKeyFound(String key, PropertySource<?> propertySource, Object value) {
110111
if (logger.isDebugEnabled()) {
111-
logger.debug(String.format("Found key '%s' in [%s] with type [%s]",
112-
key, propertySource.getName(), value.getClass().getSimpleName()));
112+
logger.debug("Found key '" + key + "' in PropertySource '" + propertySource.getName() +
113+
"' with value of type " + value.getClass().getSimpleName());
113114
}
114115
}
115116

spring-core/src/main/java/org/springframework/core/env/SystemEnvironmentPropertySource.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -91,8 +91,8 @@ public boolean containsProperty(String name) {
9191
public Object getProperty(String name) {
9292
String actualName = resolvePropertyName(name);
9393
if (logger.isDebugEnabled() && !name.equals(actualName)) {
94-
logger.debug(String.format("PropertySource [%s] does not contain '%s', but found equivalent '%s'",
95-
getName(), name, actualName));
94+
logger.debug("PropertySource '" + getName() + "' does not contain property '" + name +
95+
"', but found equivalent '" + actualName + "'");
9696
}
9797
return super.getProperty(actualName);
9898
}

0 commit comments

Comments
 (0)