Polish loggers

This commit is contained in:
Phillip Webb 2016-12-20 19:07:16 -08:00
parent e6097fb3e4
commit 38f7389eab
8 changed files with 41 additions and 35 deletions

View File

@ -38,7 +38,8 @@ import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
*/ */
class CloudFoundrySecurityInterceptor extends HandlerInterceptorAdapter { class CloudFoundrySecurityInterceptor extends HandlerInterceptorAdapter {
protected final Log logger = LogFactory.getLog(getClass()); private static final Log logger = LogFactory
.getLog(CloudFoundrySecurityInterceptor.class);
private final TokenValidator tokenValidator; private final TokenValidator tokenValidator;
@ -74,7 +75,7 @@ class CloudFoundrySecurityInterceptor extends HandlerInterceptorAdapter {
check(request, mvcEndpoint); check(request, mvcEndpoint);
} }
catch (CloudFoundryAuthorizationException ex) { catch (CloudFoundryAuthorizationException ex) {
this.logger.error(ex); logger.error(ex);
response.setContentType(MediaType.APPLICATION_JSON.toString()); response.setContentType(MediaType.APPLICATION_JSON.toString());
response.getWriter() response.getWriter()
.write("{\"security_error\":\"" + ex.getMessage() + "\"}"); .write("{\"security_error\":\"" + ex.getMessage() + "\"}");

View File

@ -18,9 +18,6 @@ package org.springframework.boot.autoconfigure.security.oauth2.resource;
import java.util.List; import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken; import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.GrantedAuthority; import org.springframework.security.core.GrantedAuthority;
@ -43,8 +40,6 @@ import org.springframework.social.oauth2.AccessGrant;
*/ */
public class SpringSocialTokenServices implements ResourceServerTokenServices { public class SpringSocialTokenServices implements ResourceServerTokenServices {
protected final Log logger = LogFactory.getLog(getClass());
private final OAuth2ConnectionFactory<?> connectionFactory; private final OAuth2ConnectionFactory<?> connectionFactory;
private final String clientId; private final String clientId;

View File

@ -81,20 +81,24 @@ public class AutoConfigurationReportLoggingInitializerTests {
given(this.log.isDebugEnabled()).willReturn(debug); given(this.log.isDebugEnabled()).willReturn(debug);
willAnswer(new Answer<Object>() { willAnswer(new Answer<Object>() {
@Override @Override
public Object answer(InvocationOnMock invocation) throws Throwable { public Object answer(InvocationOnMock invocation) throws Throwable {
return AutoConfigurationReportLoggingInitializerTests.this.debugLog return AutoConfigurationReportLoggingInitializerTests.this.debugLog
.add(String.valueOf(invocation.getArguments()[0])); .add(String.valueOf(invocation.getArguments()[0]));
} }
}).given(this.log).debug(anyObject()); }).given(this.log).debug(anyObject());
given(this.log.isInfoEnabled()).willReturn(info); given(this.log.isInfoEnabled()).willReturn(info);
willAnswer(new Answer<Object>() { willAnswer(new Answer<Object>() {
@Override @Override
public Object answer(InvocationOnMock invocation) throws Throwable { public Object answer(InvocationOnMock invocation) throws Throwable {
return AutoConfigurationReportLoggingInitializerTests.this.infoLog return AutoConfigurationReportLoggingInitializerTests.this.infoLog
.add(String.valueOf(invocation.getArguments()[0])); .add(String.valueOf(invocation.getArguments()[0]));
} }
}).given(this.log).info(anyObject()); }).given(this.log).info(anyObject());
LogFactory.releaseAll(); LogFactory.releaseAll();

View File

@ -49,7 +49,7 @@ import org.springframework.util.Assert;
*/ */
public class ImageBanner implements Banner { public class ImageBanner implements Banner {
private static final Log log = LogFactory.getLog(ImageBanner.class); private static final Log logger = LogFactory.getLog(ImageBanner.class);
private static final double[] RGB_WEIGHT = { 0.2126d, 0.7152d, 0.0722d }; private static final double[] RGB_WEIGHT = { 0.2126d, 0.7152d, 0.0722d };
@ -76,9 +76,9 @@ public class ImageBanner implements Banner {
printBanner(environment, out); printBanner(environment, out);
} }
catch (Throwable ex) { catch (Throwable ex) {
log.warn("Image banner not printable: " + this.image + " (" + ex.getClass() logger.warn("Image banner not printable: " + this.image + " (" + ex.getClass()
+ ": '" + ex.getMessage() + "')"); + ": '" + ex.getMessage() + "')");
log.debug("Image banner printing failure", ex); logger.debug("Image banner printing failure", ex);
} }
finally { finally {
if (headless == null) { if (headless == null) {

View File

@ -58,7 +58,8 @@ public class PropertiesConfigurationFactory<T>
private static final char[] TARGET_NAME_DELIMITERS = { '_', '.' }; private static final char[] TARGET_NAME_DELIMITERS = { '_', '.' };
private final Log logger = LogFactory.getLog(getClass()); private static final Log logger = LogFactory
.getLog(PropertiesConfigurationFactory.class);
private boolean ignoreUnknownFields = true; private boolean ignoreUnknownFields = true;
@ -228,8 +229,8 @@ public class PropertiesConfigurationFactory<T>
public void bindPropertiesToTarget() throws BindException { public void bindPropertiesToTarget() throws BindException {
Assert.state(this.propertySources != null, "PropertySources should not be null"); Assert.state(this.propertySources != null, "PropertySources should not be null");
try { try {
if (this.logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
this.logger.trace("Property Sources: " + this.propertySources); logger.trace("Property Sources: " + this.propertySources);
} }
this.hasBeenBound = true; this.hasBeenBound = true;
@ -239,7 +240,8 @@ public class PropertiesConfigurationFactory<T>
if (this.exceptionIfInvalid) { if (this.exceptionIfInvalid) {
throw ex; throw ex;
} }
this.logger.error("Failed to load Properties validation bean. " PropertiesConfigurationFactory.logger
.error("Failed to load Properties validation bean. "
+ "Your Properties may be invalid.", ex); + "Your Properties may be invalid.", ex);
} }
} }
@ -340,10 +342,10 @@ public class PropertiesConfigurationFactory<T>
dataBinder.validate(); dataBinder.validate();
BindingResult errors = dataBinder.getBindingResult(); BindingResult errors = dataBinder.getBindingResult();
if (errors.hasErrors()) { if (errors.hasErrors()) {
this.logger.error("Properties configuration failed validation"); logger.error("Properties configuration failed validation");
for (ObjectError error : errors.getAllErrors()) { for (ObjectError error : errors.getAllErrors()) {
this.logger logger.error(
.error(this.messageSource != null this.messageSource != null
? this.messageSource.getMessage(error, ? this.messageSource.getMessage(error,
Locale.getDefault()) + " (" + error + ")" Locale.getDefault()) + " (" + error + ")"
: error); : error);

View File

@ -52,7 +52,7 @@ import org.springframework.validation.Validator;
public class YamlConfigurationFactory<T> public class YamlConfigurationFactory<T>
implements FactoryBean<T>, MessageSourceAware, InitializingBean { implements FactoryBean<T>, MessageSourceAware, InitializingBean {
private final Log logger = LogFactory.getLog(getClass()); private static final Log logger = LogFactory.getLog(YamlConfigurationFactory.class);
private final Class<?> type; private final Class<?> type;
@ -137,8 +137,8 @@ public class YamlConfigurationFactory<T>
Assert.state(this.yaml != null, "Yaml document should not be null: " Assert.state(this.yaml != null, "Yaml document should not be null: "
+ "either set it directly or set the resource to load it from"); + "either set it directly or set the resource to load it from");
try { try {
if (this.logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
this.logger.trace(String.format("Yaml document is %n%s", this.yaml)); logger.trace(String.format("Yaml document is %n%s", this.yaml));
} }
Constructor constructor = new YamlJavaBeanPropertyConstructor(this.type, Constructor constructor = new YamlJavaBeanPropertyConstructor(this.type,
this.propertyAliases); this.propertyAliases);
@ -151,7 +151,7 @@ public class YamlConfigurationFactory<T>
if (this.exceptionIfInvalid) { if (this.exceptionIfInvalid) {
throw ex; throw ex;
} }
this.logger.error("Failed to load YAML validation bean. " logger.error("Failed to load YAML validation bean. "
+ "Your YAML file may be invalid.", ex); + "Your YAML file may be invalid.", ex);
} }
} }
@ -161,13 +161,9 @@ public class YamlConfigurationFactory<T>
"configuration"); "configuration");
this.validator.validate(this.configuration, errors); this.validator.validate(this.configuration, errors);
if (errors.hasErrors()) { if (errors.hasErrors()) {
this.logger.error("YAML configuration failed validation"); logger.error("YAML configuration failed validation");
for (ObjectError error : errors.getAllErrors()) { for (ObjectError error : errors.getAllErrors()) {
this.logger logger.error(getErrorMessage(error));
.error(this.messageSource != null
? this.messageSource.getMessage(error,
Locale.getDefault()) + " (" + error + ")"
: error);
} }
if (this.exceptionIfInvalid) { if (this.exceptionIfInvalid) {
BindException summary = new BindException(errors); BindException summary = new BindException(errors);
@ -176,6 +172,14 @@ public class YamlConfigurationFactory<T>
} }
} }
private Object getErrorMessage(ObjectError error) {
if (this.messageSource != null) {
Locale locale = Locale.getDefault();
return this.messageSource.getMessage(error, locale) + " (" + error + ")";
}
return error;
}
@Override @Override
public Class<?> getObjectType() { public Class<?> getObjectType() {
if (this.configuration == null) { if (this.configuration == null) {

View File

@ -48,7 +48,7 @@ import org.springframework.util.ReflectionUtils;
*/ */
public final class FailureAnalyzers { public final class FailureAnalyzers {
private static final Log log = LogFactory.getLog(FailureAnalyzers.class); private static final Log logger = LogFactory.getLog(FailureAnalyzers.class);
private final ClassLoader classLoader; private final ClassLoader classLoader;
@ -82,7 +82,7 @@ public final class FailureAnalyzers {
analyzers.add((FailureAnalyzer) constructor.newInstance()); analyzers.add((FailureAnalyzer) constructor.newInstance());
} }
catch (Throwable ex) { catch (Throwable ex) {
log.trace("Failed to load " + analyzerName, ex); logger.trace("Failed to load " + analyzerName, ex);
} }
} }
AnnotationAwareOrderComparator.sort(analyzers); AnnotationAwareOrderComparator.sort(analyzers);

View File

@ -42,17 +42,17 @@ public final class ClasspathLoggingApplicationListener
private static final int ORDER = LoggingApplicationListener.DEFAULT_ORDER + 1; private static final int ORDER = LoggingApplicationListener.DEFAULT_ORDER + 1;
private final Log logger = LogFactory.getLog(getClass()); private static final Log logger = LogFactory
.getLog(ClasspathLoggingApplicationListener.class);
@Override @Override
public void onApplicationEvent(ApplicationEvent event) { public void onApplicationEvent(ApplicationEvent event) {
if (this.logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
if (event instanceof ApplicationEnvironmentPreparedEvent) { if (event instanceof ApplicationEnvironmentPreparedEvent) {
this.logger logger.debug("Application started with classpath: " + getClasspath());
.debug("Application started with classpath: " + getClasspath());
} }
else if (event instanceof ApplicationFailedEvent) { else if (event instanceof ApplicationFailedEvent) {
this.logger.debug( logger.debug(
"Application failed to start with classpath: " + getClasspath()); "Application failed to start with classpath: " + getClasspath());
} }
} }