From 38f7389eab7b2dd4f3758214a6aca743941658aa Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Tue, 20 Dec 2016 19:07:16 -0800 Subject: [PATCH] Polish loggers --- .../CloudFoundrySecurityInterceptor.java | 5 ++-- .../resource/SpringSocialTokenServices.java | 5 ---- ...gurationReportLoggingInitializerTests.java | 4 ++++ .../org/springframework/boot/ImageBanner.java | 6 ++--- .../bind/PropertiesConfigurationFactory.java | 18 +++++++------- .../boot/bind/YamlConfigurationFactory.java | 24 +++++++++++-------- .../boot/diagnostics/FailureAnalyzers.java | 4 ++-- .../ClasspathLoggingApplicationListener.java | 10 ++++---- 8 files changed, 41 insertions(+), 35 deletions(-) diff --git a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityInterceptor.java b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityInterceptor.java index ab13a0e148..38d0e47433 100644 --- a/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityInterceptor.java +++ b/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/cloudfoundry/CloudFoundrySecurityInterceptor.java @@ -38,7 +38,8 @@ import org.springframework.web.servlet.handler.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; @@ -74,7 +75,7 @@ class CloudFoundrySecurityInterceptor extends HandlerInterceptorAdapter { check(request, mvcEndpoint); } catch (CloudFoundryAuthorizationException ex) { - this.logger.error(ex); + logger.error(ex); response.setContentType(MediaType.APPLICATION_JSON.toString()); response.getWriter() .write("{\"security_error\":\"" + ex.getMessage() + "\"}"); diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/SpringSocialTokenServices.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/SpringSocialTokenServices.java index 6cadf238c3..f453493626 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/SpringSocialTokenServices.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/security/oauth2/resource/SpringSocialTokenServices.java @@ -18,9 +18,6 @@ package org.springframework.boot.autoconfigure.security.oauth2.resource; 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.core.AuthenticationException; import org.springframework.security.core.GrantedAuthority; @@ -43,8 +40,6 @@ import org.springframework.social.oauth2.AccessGrant; */ public class SpringSocialTokenServices implements ResourceServerTokenServices { - protected final Log logger = LogFactory.getLog(getClass()); - private final OAuth2ConnectionFactory connectionFactory; private final String clientId; diff --git a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java index 4e38cd9bba..5727d6cead 100644 --- a/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java +++ b/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/logging/AutoConfigurationReportLoggingInitializerTests.java @@ -81,20 +81,24 @@ public class AutoConfigurationReportLoggingInitializerTests { given(this.log.isDebugEnabled()).willReturn(debug); willAnswer(new Answer() { + @Override public Object answer(InvocationOnMock invocation) throws Throwable { return AutoConfigurationReportLoggingInitializerTests.this.debugLog .add(String.valueOf(invocation.getArguments()[0])); } + }).given(this.log).debug(anyObject()); given(this.log.isInfoEnabled()).willReturn(info); willAnswer(new Answer() { + @Override public Object answer(InvocationOnMock invocation) throws Throwable { return AutoConfigurationReportLoggingInitializerTests.this.infoLog .add(String.valueOf(invocation.getArguments()[0])); } + }).given(this.log).info(anyObject()); LogFactory.releaseAll(); diff --git a/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java b/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java index c6b2133e0b..0404b16ca3 100644 --- a/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java +++ b/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java @@ -49,7 +49,7 @@ import org.springframework.util.Assert; */ 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 }; @@ -76,9 +76,9 @@ public class ImageBanner implements Banner { printBanner(environment, out); } 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() + "')"); - log.debug("Image banner printing failure", ex); + logger.debug("Image banner printing failure", ex); } finally { if (headless == null) { diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java index f8bcaf0242..13f305da05 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/PropertiesConfigurationFactory.java @@ -58,7 +58,8 @@ public class PropertiesConfigurationFactory 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; @@ -228,8 +229,8 @@ public class PropertiesConfigurationFactory public void bindPropertiesToTarget() throws BindException { Assert.state(this.propertySources != null, "PropertySources should not be null"); try { - if (this.logger.isTraceEnabled()) { - this.logger.trace("Property Sources: " + this.propertySources); + if (logger.isTraceEnabled()) { + logger.trace("Property Sources: " + this.propertySources); } this.hasBeenBound = true; @@ -239,8 +240,9 @@ public class PropertiesConfigurationFactory if (this.exceptionIfInvalid) { throw ex; } - this.logger.error("Failed to load Properties validation bean. " - + "Your Properties may be invalid.", ex); + PropertiesConfigurationFactory.logger + .error("Failed to load Properties validation bean. " + + "Your Properties may be invalid.", ex); } } @@ -340,10 +342,10 @@ public class PropertiesConfigurationFactory dataBinder.validate(); BindingResult errors = dataBinder.getBindingResult(); if (errors.hasErrors()) { - this.logger.error("Properties configuration failed validation"); + logger.error("Properties configuration failed validation"); for (ObjectError error : errors.getAllErrors()) { - this.logger - .error(this.messageSource != null + logger.error( + this.messageSource != null ? this.messageSource.getMessage(error, Locale.getDefault()) + " (" + error + ")" : error); diff --git a/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java b/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java index 6ab2e551ea..fd45de3296 100644 --- a/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java +++ b/spring-boot/src/main/java/org/springframework/boot/bind/YamlConfigurationFactory.java @@ -52,7 +52,7 @@ import org.springframework.validation.Validator; public class YamlConfigurationFactory implements FactoryBean, MessageSourceAware, InitializingBean { - private final Log logger = LogFactory.getLog(getClass()); + private static final Log logger = LogFactory.getLog(YamlConfigurationFactory.class); private final Class type; @@ -137,8 +137,8 @@ public class YamlConfigurationFactory Assert.state(this.yaml != null, "Yaml document should not be null: " + "either set it directly or set the resource to load it from"); try { - if (this.logger.isTraceEnabled()) { - this.logger.trace(String.format("Yaml document is %n%s", this.yaml)); + if (logger.isTraceEnabled()) { + logger.trace(String.format("Yaml document is %n%s", this.yaml)); } Constructor constructor = new YamlJavaBeanPropertyConstructor(this.type, this.propertyAliases); @@ -151,7 +151,7 @@ public class YamlConfigurationFactory if (this.exceptionIfInvalid) { 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); } } @@ -161,13 +161,9 @@ public class YamlConfigurationFactory "configuration"); this.validator.validate(this.configuration, errors); if (errors.hasErrors()) { - this.logger.error("YAML configuration failed validation"); + logger.error("YAML configuration failed validation"); for (ObjectError error : errors.getAllErrors()) { - this.logger - .error(this.messageSource != null - ? this.messageSource.getMessage(error, - Locale.getDefault()) + " (" + error + ")" - : error); + logger.error(getErrorMessage(error)); } if (this.exceptionIfInvalid) { BindException summary = new BindException(errors); @@ -176,6 +172,14 @@ public class YamlConfigurationFactory } } + private Object getErrorMessage(ObjectError error) { + if (this.messageSource != null) { + Locale locale = Locale.getDefault(); + return this.messageSource.getMessage(error, locale) + " (" + error + ")"; + } + return error; + } + @Override public Class getObjectType() { if (this.configuration == null) { diff --git a/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java b/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java index 75e19c9e07..2f9f800e91 100644 --- a/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java +++ b/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java @@ -48,7 +48,7 @@ import org.springframework.util.ReflectionUtils; */ 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; @@ -82,7 +82,7 @@ public final class FailureAnalyzers { analyzers.add((FailureAnalyzer) constructor.newInstance()); } catch (Throwable ex) { - log.trace("Failed to load " + analyzerName, ex); + logger.trace("Failed to load " + analyzerName, ex); } } AnnotationAwareOrderComparator.sort(analyzers); diff --git a/spring-boot/src/main/java/org/springframework/boot/logging/ClasspathLoggingApplicationListener.java b/spring-boot/src/main/java/org/springframework/boot/logging/ClasspathLoggingApplicationListener.java index 3dcd654818..e31eea4080 100644 --- a/spring-boot/src/main/java/org/springframework/boot/logging/ClasspathLoggingApplicationListener.java +++ b/spring-boot/src/main/java/org/springframework/boot/logging/ClasspathLoggingApplicationListener.java @@ -42,17 +42,17 @@ public final class ClasspathLoggingApplicationListener 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 public void onApplicationEvent(ApplicationEvent event) { - if (this.logger.isDebugEnabled()) { + if (logger.isDebugEnabled()) { if (event instanceof ApplicationEnvironmentPreparedEvent) { - this.logger - .debug("Application started with classpath: " + getClasspath()); + logger.debug("Application started with classpath: " + getClasspath()); } else if (event instanceof ApplicationFailedEvent) { - this.logger.debug( + logger.debug( "Application failed to start with classpath: " + getClasspath()); } }