From 154ea3d4762d6b527170792c28e9e6ef1e29720a Mon Sep 17 00:00:00 2001 From: Florian Hopf Date: Mon, 26 Feb 2018 15:28:35 +0800 Subject: [PATCH 1/3] Logging some information on property source --- .../ConfigServicePropertySourceLocator.java | 35 ++++++++++++------- 1 file changed, 22 insertions(+), 13 deletions(-) diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index 478b4615..c7f1e781 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -16,12 +16,6 @@ package org.springframework.cloud.config.client; -import java.io.IOException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.Map; -import java.util.Map.Entry; - import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; @@ -30,13 +24,7 @@ import org.springframework.cloud.config.environment.PropertySource; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.MapPropertySource; -import org.springframework.http.HttpEntity; -import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpMethod; -import org.springframework.http.HttpRequest; -import org.springframework.http.HttpStatus; -import org.springframework.http.MediaType; -import org.springframework.http.ResponseEntity; +import org.springframework.http.*; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; @@ -48,6 +36,13 @@ import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestTemplate; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + import static org.springframework.cloud.config.client.ConfigClientProperties.STATE_HEADER; import static org.springframework.cloud.config.client.ConfigClientProperties.TOKEN_HEADER; @@ -97,6 +92,20 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator result.getName(), result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()), result.getLabel(), result.getVersion(), result.getState())); + if (logger.isDebugEnabled()) { + List propertySourceList = result.getPropertySources(); + if (propertySourceList != null) { + int propertyCount = 0; + for (PropertySource propertySource: propertySourceList) { + propertyCount += propertySource.getSource().size(); + } + logger.debug(String.format("Environment %s has %d property sources with %d properties.", + result.getName(), + result.getPropertySources().size(), + propertyCount)); + } + + } if (result.getPropertySources() != null) { // result.getPropertySources() can be null if using xml for (PropertySource source : result.getPropertySources()) { From c29eb7f7e3863bf3baee23605528e69ae1757e2d Mon Sep 17 00:00:00 2001 From: Florian Hopf Date: Tue, 27 Feb 2018 10:19:43 +0800 Subject: [PATCH 2/3] extracted method, cleaned up imports --- .../ConfigServicePropertySourceLocator.java | 62 +++++++++++-------- 1 file changed, 36 insertions(+), 26 deletions(-) diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index c7f1e781..5a09af09 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -16,6 +16,13 @@ package org.springframework.cloud.config.client; +import java.io.IOException; +import java.util.Arrays; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; + import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.cloud.bootstrap.config.PropertySourceLocator; @@ -24,7 +31,13 @@ import org.springframework.cloud.config.environment.PropertySource; import org.springframework.core.annotation.Order; import org.springframework.core.env.CompositePropertySource; import org.springframework.core.env.MapPropertySource; -import org.springframework.http.*; +import org.springframework.http.HttpEntity; +import org.springframework.http.HttpHeaders; +import org.springframework.http.HttpMethod; +import org.springframework.http.HttpRequest; +import org.springframework.http.HttpStatus; +import org.springframework.http.MediaType; +import org.springframework.http.ResponseEntity; import org.springframework.http.client.ClientHttpRequestExecution; import org.springframework.http.client.ClientHttpRequestInterceptor; import org.springframework.http.client.ClientHttpResponse; @@ -36,13 +49,6 @@ import org.springframework.web.client.HttpClientErrorException; import org.springframework.web.client.HttpServerErrorException; import org.springframework.web.client.RestTemplate; -import java.io.IOException; -import java.util.Arrays; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - import static org.springframework.cloud.config.client.ConfigClientProperties.STATE_HEADER; import static org.springframework.cloud.config.client.ConfigClientProperties.TOKEN_HEADER; @@ -88,24 +94,7 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator Environment result = getRemoteEnvironment(restTemplate, properties, label.trim(), state); if (result != null) { - logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s", - result.getName(), - result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()), - result.getLabel(), result.getVersion(), result.getState())); - if (logger.isDebugEnabled()) { - List propertySourceList = result.getPropertySources(); - if (propertySourceList != null) { - int propertyCount = 0; - for (PropertySource propertySource: propertySourceList) { - propertyCount += propertySource.getSource().size(); - } - logger.debug(String.format("Environment %s has %d property sources with %d properties.", - result.getName(), - result.getPropertySources().size(), - propertyCount)); - } - - } + log(result); if (result.getPropertySources() != null) { // result.getPropertySources() can be null if using xml for (PropertySource source : result.getPropertySources()) { @@ -148,6 +137,27 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator } + private void log(Environment result) { + logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s", + result.getName(), + result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()), + result.getLabel(), result.getVersion(), result.getState())); + if (logger.isDebugEnabled()) { + List propertySourceList = result.getPropertySources(); + if (propertySourceList != null) { + int propertyCount = 0; + for (PropertySource propertySource: propertySourceList) { + propertyCount += propertySource.getSource().size(); + } + logger.debug(String.format("Environment %s has %d property sources with %d properties.", + result.getName(), + result.getPropertySources().size(), + propertyCount)); + } + + } + } + private void putValue(HashMap map, String key, String value) { if (StringUtils.hasText(value)) { map.put(key, value); From 94d9b39b42a2aed47e2169f0a40e12a0d4e25ba3 Mon Sep 17 00:00:00 2001 From: Florian Hopf Date: Tue, 27 Feb 2018 11:01:15 +0800 Subject: [PATCH 3/3] tabs instead of spaces, checking for log level --- .../ConfigServicePropertySourceLocator.java | 34 ++++++++++--------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java index 5a09af09..11fdd8f4 100644 --- a/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java +++ b/spring-cloud-config-client/src/main/java/org/springframework/cloud/config/client/ConfigServicePropertySourceLocator.java @@ -138,24 +138,26 @@ public class ConfigServicePropertySourceLocator implements PropertySourceLocator } private void log(Environment result) { - logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s", - result.getName(), - result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()), - result.getLabel(), result.getVersion(), result.getState())); + if (logger.isInfoEnabled()) { + logger.info(String.format("Located environment: name=%s, profiles=%s, label=%s, version=%s, state=%s", + result.getName(), + result.getProfiles() == null ? "" : Arrays.asList(result.getProfiles()), + result.getLabel(), result.getVersion(), result.getState())); + } if (logger.isDebugEnabled()) { - List propertySourceList = result.getPropertySources(); - if (propertySourceList != null) { - int propertyCount = 0; - for (PropertySource propertySource: propertySourceList) { - propertyCount += propertySource.getSource().size(); - } - logger.debug(String.format("Environment %s has %d property sources with %d properties.", - result.getName(), - result.getPropertySources().size(), - propertyCount)); - } + List propertySourceList = result.getPropertySources(); + if (propertySourceList != null) { + int propertyCount = 0; + for (PropertySource propertySource: propertySourceList) { + propertyCount += propertySource.getSource().size(); + } + logger.debug(String.format("Environment %s has %d property sources with %d properties.", + result.getName(), + result.getPropertySources().size(), + propertyCount)); + } - } + } } private void putValue(HashMap map, String key, String value) {