Skip to content

Commit 48ad907

Browse files
authored
Merge pull request #101 from microsphere-projects/copilot/refactor-logging-to-check-trace-enabled
Refactor: guard all logger calls with isXxxEnabled() checks
2 parents dd2ec8f + 7df91fb commit 48ad907

21 files changed

Lines changed: 166 additions & 38 deletions

File tree

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebServiceRegistryAutoConfiguration.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,15 @@ private void attachWebMappingsMetadata(Registration registration, Collection<Web
8989
attachMetadata(getContextPath(), registration, mappings);
9090
}
9191

92-
private void excludeMappings(Set<WebEndpointMapping> mappings) {
92+
void excludeMappings(Collection<WebEndpointMapping> mappings) {
9393
Iterator<WebEndpointMapping> iterator = mappings.iterator();
9494
while (iterator.hasNext()) {
9595
WebEndpointMapping mapping = iterator.next();
9696
String[] patterns = mapping.getPatterns();
9797
if (isExcludedMapping(mapping, patterns) || isActuatorWebEndpointMapping(mapping, patterns)) {
98-
logger.trace("The '{}' was excluded", mapping);
98+
if (logger.isTraceEnabled()) {
99+
logger.trace("The '{}' was excluded", mapping);
100+
}
99101
iterator.remove();
100102
}
101103
}

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/endpoint/ServiceDeregistrationEndpoint.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,14 @@ public boolean stop() {
3333
boolean isRunning = isRunning();
3434
if (isRunning) {
3535
serviceRegistry.deregister(registration);
36-
logger.info("Service[name : '{}'] is deregistered!", applicationName);
36+
if (logger.isInfoEnabled()) {
37+
logger.info("Service[name : '{}'] is deregistered!", applicationName);
38+
}
3739
setRunning(false);
3840
} else {
39-
logger.warn("Service[name : '{}'] is not registered, deregistration can't be executed!", applicationName);
41+
if (logger.isWarnEnabled()) {
42+
logger.warn("Service[name : '{}'] is not registered, deregistration can't be executed!", applicationName);
43+
}
4044
}
4145
return isRunning;
4246
}

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/registry/endpoint/ServiceRegistrationEndpoint.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,13 @@ public boolean start() {
7272
if (!isRunning) {
7373
serviceRegistry.register(registration);
7474
setRunning(true);
75-
logger.info("Service[name : '{}'] is registered!", applicationName);
75+
if (logger.isInfoEnabled()) {
76+
logger.info("Service[name : '{}'] is registered!", applicationName);
77+
}
7678
} else {
77-
logger.warn("Service[name : '{}'] was registered!", applicationName);
79+
if (logger.isWarnEnabled()) {
80+
logger.warn("Service[name : '{}'] was registered!", applicationName);
81+
}
7882
}
7983
return isRunning;
8084
}

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/client/service/util/ServiceInstanceUtils.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,14 +86,18 @@ public static void attachMetadata(String contextPath, ServiceInstance serviceIns
8686
StringJoiner jsonBuilder = new StringJoiner(COMMA + LINE_SEPARATOR, LEFT_SQUARE_BRACKET, RIGHT_SQUARE_BRACKET);
8787
webEndpointMappings.stream().map(WebEndpointMapping::toJSON).forEach(jsonBuilder::add);
8888
String json = jsonBuilder.toString();
89-
logger.trace("Web Endpoint Mappings JSON: \n{}", json);
89+
if (logger.isTraceEnabled()) {
90+
logger.trace("Web Endpoint Mappings JSON: \n{}", json);
91+
}
9092
json = json.replace(LINE_SEPARATOR, EMPTY_STRING);
9193
String encodedJson = encode(json);
9294

9395
metadata.put(WEB_CONTEXT_PATH_METADATA_NAME, contextPath);
9496
metadata.put(WEB_MAPPINGS_METADATA_NAME, encodedJson);
95-
logger.trace("ServiceInstance's metadata :");
96-
metadata.forEach((name, value) -> logger.trace("{} : {}", name, value));
97+
if (logger.isTraceEnabled()) {
98+
logger.trace("ServiceInstance's metadata :");
99+
metadata.forEach((name, value) -> logger.trace("{} : {}", name, value));
100+
}
97101
}
98102

99103
/**

microsphere-spring-cloud-commons/src/main/java/io/microsphere/spring/cloud/fault/tolerance/tomcat/event/TomcatDynamicConfigurationListener.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,13 +119,17 @@ private void initCurrentServerProperties() {
119119
@Override
120120
public void onApplicationEvent(EnvironmentChangeEvent event) {
121121
if (!isSourceFrom(event)) {
122-
logger.trace("Current context[id : '{}'] receives the other changed property names : {}", context.getId(), event.getKeys());
122+
if (logger.isTraceEnabled()) {
123+
logger.trace("Current context[id : '{}'] receives the other changed property names : {}", context.getId(), event.getKeys());
124+
}
123125
return;
124126
}
125127

126128
Set<String> serverPropertyNames = filterServerPropertyNames(event);
127129
if (serverPropertyNames.isEmpty()) {
128-
logger.trace("Current context[id : '{}'] does not receive the property change of ServerProperties, keys : {}", context.getId(), event.getKeys());
130+
if (logger.isTraceEnabled()) {
131+
logger.trace("Current context[id : '{}'] does not receive the property change of ServerProperties, keys : {}", context.getId(), event.getKeys());
132+
}
129133
return;
130134
}
131135

@@ -151,7 +155,9 @@ private boolean isServerPropertyName(String propertyName) {
151155

152156
private void configureTomcatIfChanged(Set<String> serverPropertyNames) {
153157
ServerProperties refreshableServerProperties = getRefreshableServerProperties(serverPropertyNames);
154-
logger.trace("The ServerProperties property is changed to: {}", getProperties(environment, serverPropertyNames));
158+
if (logger.isTraceEnabled()) {
159+
logger.trace("The ServerProperties property is changed to: {}", getProperties(environment, serverPropertyNames));
160+
}
155161
configureConnector(refreshableServerProperties);
156162
// Reset current ServerProperties
157163
initCurrentServerProperties();

microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebMvcServiceRegistryAutoConfigurationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828
@EnableWebMvcExtension
2929
@EnableAutoConfiguration
3030
class WebMvcServiceRegistryAutoConfigurationTest extends WebServiceRegistryAutoConfigurationTest {
31-
}
31+
}

microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/autoconfigure/WebServiceRegistryAutoConfigurationTest.java

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package io.microsphere.spring.cloud.client.service.registry.autoconfigure;
1919

2020

21+
import io.microsphere.logging.test.jupiter.LoggingLevelsTest;
2122
import io.microsphere.spring.test.web.controller.TestController;
2223
import io.microsphere.spring.web.metadata.WebEndpointMapping;
2324
import org.junit.jupiter.api.Test;
@@ -28,13 +29,16 @@
2829
import java.util.Collection;
2930
import java.util.Map;
3031

32+
import static io.microsphere.collection.ListUtils.newLinkedList;
33+
import static io.microsphere.collection.Lists.ofList;
3134
import static io.microsphere.spring.cloud.client.service.registry.constants.InstanceConstants.WEB_CONTEXT_PATH_METADATA_NAME;
3235
import static io.microsphere.spring.cloud.client.service.registry.constants.InstanceConstants.WEB_MAPPINGS_METADATA_NAME;
3336
import static io.microsphere.spring.cloud.client.service.util.ServiceInstanceUtils.getWebEndpointMappings;
3437
import static org.junit.jupiter.api.Assertions.assertEquals;
3538
import static org.junit.jupiter.api.Assertions.assertNotNull;
3639
import static org.junit.jupiter.api.Assertions.assertTrue;
3740
import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT;
41+
import static org.springframework.http.HttpMethod.GET;
3842

3943
/**
4044
* {@link WebServiceRegistryAutoConfiguration} Test
@@ -56,13 +60,32 @@ abstract class WebServiceRegistryAutoConfigurationTest {
5660
@Autowired
5761
private Registration registration;
5862

63+
@Autowired
64+
private WebServiceRegistryAutoConfiguration autoConfiguration;
65+
5966
@Test
6067
void test() {
61-
Map<String, String> metadata = registration.getMetadata();
68+
Map<String, String> metadata = this.registration.getMetadata();
6269
assertEquals("", metadata.get(WEB_CONTEXT_PATH_METADATA_NAME));
6370
assertNotNull(metadata.get(WEB_MAPPINGS_METADATA_NAME));
6471

65-
Collection<WebEndpointMapping> webEndpointMappings = getWebEndpointMappings(registration);
72+
Collection<WebEndpointMapping> webEndpointMappings = getWebEndpointMappings(this.registration);
6673
assertTrue(webEndpointMappings.size() >= 6);
6774
}
75+
76+
@Test
77+
@LoggingLevelsTest(levels = "ERROR")
78+
void testExcludeMappings() {
79+
WebEndpointMapping webEndpointMapping = WebEndpointMapping
80+
.webmvc()
81+
.endpoint(this)
82+
.methods(GET)
83+
.pattern("/actuator/test")
84+
.build();
85+
86+
Collection<WebEndpointMapping> webEndpointMappings = newLinkedList(ofList(webEndpointMapping));
87+
88+
this.autoConfiguration.excludeMappings(webEndpointMappings);
89+
assertTrue(webEndpointMappings.isEmpty());
90+
}
6891
}

microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/endpoint/ServiceDeregistrationEndpointTest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
package io.microsphere.spring.cloud.client.service.registry.endpoint;
1919

2020

21+
import io.microsphere.logging.test.jupiter.LoggingLevelsTest;
2122
import org.junit.jupiter.api.Test;
2223
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
2425
import org.springframework.test.context.ContextConfiguration;
2526

26-
import static org.junit.jupiter.api.Assertions.assertFalse;
27-
import static org.junit.jupiter.api.Assertions.assertTrue;
27+
import static org.junit.jupiter.api.Assertions.assertEquals;
2828

2929
/**
3030
* {@link ServiceDeregistrationEndpoint} Test
@@ -47,9 +47,10 @@ class ServiceDeregistrationEndpointTest extends BaseServiceRegistrationEndpointT
4747
private ServiceDeregistrationEndpoint serviceDeregistrationEndpoint;
4848

4949
@Test
50+
@LoggingLevelsTest(levels = "ERROR")
5051
void testStop() {
51-
assertFalse(this.serviceDeregistrationEndpoint.stop());
52-
assertFalse(this.serviceRegistrationEndpoint.start());
53-
assertTrue(this.serviceDeregistrationEndpoint.stop());
52+
assertEquals(this.serviceDeregistrationEndpoint.isRunning(), this.serviceDeregistrationEndpoint.stop());
53+
assertEquals(this.serviceRegistrationEndpoint.isRunning(), this.serviceRegistrationEndpoint.start());
54+
assertEquals(this.serviceDeregistrationEndpoint.isRunning(), this.serviceDeregistrationEndpoint.stop());
5455
}
5556
}

microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/registry/endpoint/ServiceRegistrationEndpointTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
package io.microsphere.spring.cloud.client.service.registry.endpoint;
1919

2020

21+
import io.microsphere.logging.test.jupiter.LoggingLevelsTest;
2122
import org.junit.jupiter.api.Test;
2223
import org.springframework.beans.factory.annotation.Autowired;
2324
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
@@ -28,10 +29,8 @@
2829
import static java.lang.Boolean.TRUE;
2930
import static java.lang.Integer.valueOf;
3031
import static org.junit.jupiter.api.Assertions.assertEquals;
31-
import static org.junit.jupiter.api.Assertions.assertFalse;
3232
import static org.junit.jupiter.api.Assertions.assertNull;
3333
import static org.junit.jupiter.api.Assertions.assertSame;
34-
import static org.junit.jupiter.api.Assertions.assertTrue;
3534

3635
/**
3736
* {@link ServiceRegistrationEndpoint} Test
@@ -64,8 +63,9 @@ void testMetadata() {
6463
}
6564

6665
@Test
66+
@LoggingLevelsTest(levels = "ERROR")
6767
void testStart() {
68-
assertFalse(this.endpoint.start());
69-
assertTrue(this.endpoint.start());
68+
assertEquals(this.endpoint.isRunning(), this.endpoint.start());
69+
assertEquals(this.endpoint.isRunning(), this.endpoint.start());
7070
}
7171
}

microsphere-spring-cloud-commons/src/test/java/io/microsphere/spring/cloud/client/service/util/ServiceInstanceUtilsTest.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020

2121
import io.microsphere.json.JSONObject;
22+
import io.microsphere.logging.test.jupiter.LoggingLevelsTest;
2223
import io.microsphere.spring.web.metadata.WebEndpointMapping;
2324
import io.microsphere.spring.web.metadata.WebEndpointMapping.Builder;
2425
import org.junit.jupiter.api.BeforeEach;
@@ -82,6 +83,7 @@ void setUp() {
8283
}
8384

8485
@Test
86+
@LoggingLevelsTest(levels = "ERROR")
8587
void testAttachMetadata() {
8688
attachMetadata(this.context, this.serviceInstance, this.webEndpointMappings);
8789
assertEquals(this.context, getMetadata(this.serviceInstance, WEB_CONTEXT_PATH_METADATA_NAME));

0 commit comments

Comments
 (0)