Consistent logging in Environment and PropertySource implementations

Issue: SPR-15825
This commit is contained in:
Juergen Hoeller 2017-07-27 16:02:18 +02:00
parent 3cef5a1294
commit fac83b2e7c
8 changed files with 54 additions and 71 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -121,9 +121,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
*/ */
public AbstractEnvironment() { public AbstractEnvironment() {
customizePropertySources(this.propertySources); customizePropertySources(this.propertySources);
if (this.logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
this.logger.debug(String.format( logger.debug("Initialized " + getClass().getSimpleName() + " with PropertySources " + this.propertySources);
"Initialized %s with PropertySources %s", getClass().getSimpleName(), this.propertySources));
} }
} }
@ -262,8 +261,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
@Override @Override
public void addActiveProfile(String profile) { public void addActiveProfile(String profile) {
if (this.logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
this.logger.debug(String.format("Activating profile '%s'", profile)); logger.debug("Activating profile '" + profile + "'");
} }
validateProfile(profile); validateProfile(profile);
doGetActiveProfiles(); doGetActiveProfiles();
@ -393,9 +392,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
} }
catch (AccessControlException ex) { catch (AccessControlException ex) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("Caught AccessControlException when accessing system " + logger.info("Caught AccessControlException when accessing system environment variable '" +
"environment variable [%s]; its value will be returned [null]. Reason: %s", attributeName + "'; its value will be returned [null]. Reason: " + ex.getMessage());
attributeName, ex.getMessage()));
} }
return null; return null;
} }
@ -434,9 +432,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
} }
catch (AccessControlException ex) { catch (AccessControlException ex) {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info(String.format("Caught AccessControlException when accessing system " + logger.info("Caught AccessControlException when accessing system property '" +
"property [%s]; its value will be returned [null]. Reason: %s", attributeName + "'; its value will be returned [null]. Reason: " + ex.getMessage());
attributeName, ex.getMessage()));
} }
return null; return null;
} }
@ -569,9 +566,8 @@ public abstract class AbstractEnvironment implements ConfigurableEnvironment {
@Override @Override
public String toString() { public String toString() {
return String.format("%s {activeProfiles=%s, defaultProfiles=%s, propertySources=%s}", return getClass().getSimpleName() + " {activeProfiles=" + this.activeProfiles +
getClass().getSimpleName(), this.activeProfiles, this.defaultProfiles, ", defaultProfiles=" + this.defaultProfiles + ", propertySources=" + this.propertySources + "}";
this.propertySources);
} }
} }

View File

@ -180,7 +180,7 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
public String getRequiredProperty(String key) throws IllegalStateException { public String getRequiredProperty(String key) throws IllegalStateException {
String value = getProperty(key); String value = getProperty(key);
if (value == null) { if (value == null) {
throw new IllegalStateException(String.format("required key [%s] not found", key)); throw new IllegalStateException("Required key '" + key + "' not found");
} }
return value; return value;
} }
@ -189,7 +189,7 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
public <T> T getRequiredProperty(String key, Class<T> valueType) throws IllegalStateException { public <T> T getRequiredProperty(String key, Class<T> valueType) throws IllegalStateException {
T value = getProperty(key, valueType); T value = getProperty(key, valueType);
if (value == null) { if (value == null) {
throw new IllegalStateException(String.format("required key [%s] not found", key)); throw new IllegalStateException("Required key '" + key + "' not found");
} }
return value; return value;
} }
@ -233,7 +233,7 @@ public abstract class AbstractPropertyResolver implements ConfigurablePropertyRe
} }
private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) { private String doResolvePlaceholders(String text, PropertyPlaceholderHelper helper) {
return helper.replacePlaceholders(text, placeholderName -> getPropertyAsRawString(placeholderName)); return helper.replacePlaceholders(text, this::getPropertyAsRawString);
} }
/** /**

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -119,8 +119,7 @@ public class CompositePropertySource extends EnumerablePropertySource<Object> {
@Override @Override
public String toString() { public String toString() {
return String.format("%s [name='%s', propertySources=%s]", return getClass().getSimpleName() + " {name='" + this.name + "', propertySources=" + this.propertySources + "}";
getClass().getSimpleName(), this.name, this.propertySources);
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -33,6 +33,17 @@ public class MissingRequiredPropertiesException extends IllegalStateException {
private final Set<String> missingRequiredProperties = new LinkedHashSet<>(); private final Set<String> missingRequiredProperties = new LinkedHashSet<>();
void addMissingRequiredProperty(String key) {
this.missingRequiredProperties.add(key);
}
@Override
public String getMessage() {
return "The following properties were declared as required but could not be resolved: " +
getMissingRequiredProperties();
}
/** /**
* Return the set of properties marked as required but not present * Return the set of properties marked as required but not present
* upon validation. * upon validation.
@ -40,17 +51,7 @@ public class MissingRequiredPropertiesException extends IllegalStateException {
* @see ConfigurablePropertyResolver#validateRequiredProperties() * @see ConfigurablePropertyResolver#validateRequiredProperties()
*/ */
public Set<String> getMissingRequiredProperties() { public Set<String> getMissingRequiredProperties() {
return missingRequiredProperties; return this.missingRequiredProperties;
} }
void addMissingRequiredProperty(String key) {
missingRequiredProperties.add(key);
}
@Override
public String getMessage() {
return String.format(
"The following properties were declared as required but could " +
"not be resolved: %s", this.getMissingRequiredProperties());
}
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -24,7 +24,6 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
import org.springframework.lang.Nullable; import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;
/** /**
* Default implementation of the {@link PropertySources} interface. * Default implementation of the {@link PropertySources} interface.
@ -95,8 +94,7 @@ public class MutablePropertySources implements PropertySources {
*/ */
public void addFirst(PropertySource<?> propertySource) { public void addFirst(PropertySource<?> propertySource) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Adding [%s] PropertySource with highest search precedence", logger.debug("Adding PropertySource '" + propertySource.getName() + "' with highest search precedence");
propertySource.getName()));
} }
removeIfPresent(propertySource); removeIfPresent(propertySource);
this.propertySourceList.add(0, propertySource); this.propertySourceList.add(0, propertySource);
@ -107,8 +105,7 @@ public class MutablePropertySources implements PropertySources {
*/ */
public void addLast(PropertySource<?> propertySource) { public void addLast(PropertySource<?> propertySource) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Adding [%s] PropertySource with lowest search precedence", logger.debug("Adding PropertySource '" + propertySource.getName() + "' with lowest search precedence");
propertySource.getName()));
} }
removeIfPresent(propertySource); removeIfPresent(propertySource);
this.propertySourceList.add(propertySource); this.propertySourceList.add(propertySource);
@ -120,8 +117,8 @@ public class MutablePropertySources implements PropertySources {
*/ */
public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) { public void addBefore(String relativePropertySourceName, PropertySource<?> propertySource) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Adding [%s] PropertySource with search precedence immediately higher than [%s]", logger.debug("Adding PropertySource '" + propertySource.getName() +
propertySource.getName(), relativePropertySourceName)); "' with search precedence immediately higher than '" + relativePropertySourceName + "'");
} }
assertLegalRelativeAddition(relativePropertySourceName, propertySource); assertLegalRelativeAddition(relativePropertySourceName, propertySource);
removeIfPresent(propertySource); removeIfPresent(propertySource);
@ -135,8 +132,8 @@ public class MutablePropertySources implements PropertySources {
*/ */
public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) { public void addAfter(String relativePropertySourceName, PropertySource<?> propertySource) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Adding [%s] PropertySource with search precedence immediately lower than [%s]", logger.debug("Adding PropertySource '" + propertySource.getName() +
propertySource.getName(), relativePropertySourceName)); "' with search precedence immediately lower than '" + relativePropertySourceName + "'");
} }
assertLegalRelativeAddition(relativePropertySourceName, propertySource); assertLegalRelativeAddition(relativePropertySourceName, propertySource);
removeIfPresent(propertySource); removeIfPresent(propertySource);
@ -158,7 +155,7 @@ public class MutablePropertySources implements PropertySources {
@Nullable @Nullable
public PropertySource<?> remove(String name) { public PropertySource<?> remove(String name) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Removing [%s] PropertySource", name)); logger.debug("Removing PropertySource '" + name + "'");
} }
int index = this.propertySourceList.indexOf(PropertySource.named(name)); int index = this.propertySourceList.indexOf(PropertySource.named(name));
return (index != -1 ? this.propertySourceList.remove(index) : null); return (index != -1 ? this.propertySourceList.remove(index) : null);
@ -173,8 +170,7 @@ public class MutablePropertySources implements PropertySources {
*/ */
public void replace(String name, PropertySource<?> propertySource) { public void replace(String name, PropertySource<?> propertySource) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Replacing [%s] PropertySource with [%s]", logger.debug("Replacing PropertySource '" + name + "' with '" + propertySource.getName() + "'");
name, propertySource.getName()));
} }
int index = assertPresentAndGetIndex(name); int index = assertPresentAndGetIndex(name);
this.propertySourceList.set(index, propertySource); this.propertySourceList.set(index, propertySource);
@ -189,11 +185,7 @@ public class MutablePropertySources implements PropertySources {
@Override @Override
public String toString() { public String toString() {
String[] names = new String[this.size()]; return this.propertySourceList.toString();
for (int i = 0; i < size(); i++) {
names[i] = this.propertySourceList.get(i).getName();
}
return String.format("[%s]", StringUtils.arrayToCommaDelimitedString(names));
} }
/** /**
@ -203,7 +195,7 @@ public class MutablePropertySources implements PropertySources {
String newPropertySourceName = propertySource.getName(); String newPropertySourceName = propertySource.getName();
if (relativePropertySourceName.equals(newPropertySourceName)) { if (relativePropertySourceName.equals(newPropertySourceName)) {
throw new IllegalArgumentException( throw new IllegalArgumentException(
String.format("PropertySource named [%s] cannot be added relative to itself", newPropertySourceName)); "PropertySource named '" + newPropertySourceName + "' cannot be added relative to itself");
} }
} }
@ -224,14 +216,13 @@ public class MutablePropertySources implements PropertySources {
/** /**
* Assert that the named property source is present and return its index. * Assert that the named property source is present and return its index.
* @param name the {@linkplain PropertySource#getName() name of the property source} * @param name {@linkplain PropertySource#getName() name of the property source} to find
* to find
* @throws IllegalArgumentException if the named property source is not present * @throws IllegalArgumentException if the named property source is not present
*/ */
private int assertPresentAndGetIndex(String name) { private int assertPresentAndGetIndex(String name) {
int index = this.propertySourceList.indexOf(PropertySource.named(name)); int index = this.propertySourceList.indexOf(PropertySource.named(name));
if (index == -1) { if (index == -1) {
throw new IllegalArgumentException(String.format("PropertySource named [%s] does not exist", name)); throw new IllegalArgumentException("PropertySource named '" + name + "' does not exist");
} }
return index; return index;
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2014 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -157,11 +157,11 @@ public abstract class PropertySource<T> {
@Override @Override
public String toString() { public String toString() {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
return String.format("%s@%s [name='%s', properties=%s]", return getClass().getSimpleName() + "@" + System.identityHashCode(this) +
getClass().getSimpleName(), System.identityHashCode(this), this.name, this.source); " {name='" + this.name + "', properties=" + this.source + "}";
} }
else { else {
return String.format("%s [name='%s']", getClass().getSimpleName(), this.name); return getClass().getSimpleName() + " {name='" + this.name + "'}";
} }
} }
@ -242,11 +242,6 @@ public abstract class PropertySource<T> {
public String getProperty(String name) { public String getProperty(String name) {
throw new UnsupportedOperationException(USAGE_ERROR); throw new UnsupportedOperationException(USAGE_ERROR);
} }
@Override
public String toString() {
return String.format("%s [name='%s']", getClass().getSimpleName(), this.name);
}
} }
} }

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2016 the original author or authors. * Copyright 2002-2017 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -76,7 +76,8 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
if (this.propertySources != null) { if (this.propertySources != null) {
for (PropertySource<?> propertySource : this.propertySources) { for (PropertySource<?> propertySource : this.propertySources) {
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace(String.format("Searching for key '%s' in [%s]", key, propertySource.getName())); logger.trace("Searching for key '" + key + "' in PropertySource '" +
propertySource.getName() + "'");
} }
Object value = propertySource.getProperty(key); Object value = propertySource.getProperty(key);
if (value != null) { if (value != null) {
@ -89,7 +90,7 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
} }
} }
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Could not find key '%s' in any property source", key)); logger.debug("Could not find key '" + key + "' in any property source");
} }
return null; return null;
} }
@ -108,8 +109,8 @@ public class PropertySourcesPropertyResolver extends AbstractPropertyResolver {
*/ */
protected void logKeyFound(String key, PropertySource<?> propertySource, Object value) { protected void logKeyFound(String key, PropertySource<?> propertySource, Object value) {
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug(String.format("Found key '%s' in [%s] with type [%s]", logger.debug("Found key '" + key + "' in PropertySource '" + propertySource.getName() +
key, propertySource.getName(), value.getClass().getSimpleName())); "' with value of type " + value.getClass().getSimpleName());
} }
} }

View File

@ -91,8 +91,8 @@ public class SystemEnvironmentPropertySource extends MapPropertySource {
public Object getProperty(String name) { public Object getProperty(String name) {
String actualName = resolvePropertyName(name); String actualName = resolvePropertyName(name);
if (logger.isDebugEnabled() && !name.equals(actualName)) { if (logger.isDebugEnabled() && !name.equals(actualName)) {
logger.debug(String.format("PropertySource [%s] does not contain '%s', but found equivalent '%s'", logger.debug("PropertySource '" + getName() + "' does not contain property '" + name +
getName(), name, actualName)); "', but found equivalent '" + actualName + "'");
} }
return super.getProperty(actualName); return super.getProperty(actualName);
} }