Skip to content

Commit 597baf9

Browse files
committed
Polish "Optimize logger calls"
See gh-18710
1 parent 240b1f9 commit 597baf9

File tree

25 files changed

+87
-103
lines changed

25 files changed

+87
-103
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/jdbc/DataSourcePoolMetricsAutoConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@
4343
import org.springframework.boot.jdbc.DataSourceUnwrapper;
4444
import org.springframework.boot.jdbc.metadata.DataSourcePoolMetadataProvider;
4545
import org.springframework.context.annotation.Configuration;
46+
import org.springframework.core.log.LogMessage;
4647
import org.springframework.util.StringUtils;
4748

4849
/**
@@ -124,9 +125,7 @@ private void bindMetricsRegistryToHikariDataSource(HikariDataSource hikari) {
124125
hikari.setMetricsTrackerFactory(new MicrometerMetricsTrackerFactory(this.registry));
125126
}
126127
catch (Exception ex) {
127-
if (logger.isWarnEnabled()) {
128-
logger.warn("Failed to bind Hikari metrics: " + ex.getMessage());
129-
}
128+
logger.warn(LogMessage.format("Failed to bind Hikari metrics: %s", ex.getMessage()));
130129
}
131130
}
132131
}

spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.boot.actuate.health.Health;
2626
import org.springframework.boot.actuate.health.HealthIndicator;
2727
import org.springframework.boot.actuate.health.Status;
28+
import org.springframework.core.log.LogMessage;
2829
import org.springframework.util.unit.DataSize;
2930

3031
/**
@@ -62,10 +63,8 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
6263
builder.up();
6364
}
6465
else {
65-
if (logger.isWarnEnabled()) {
66-
logger.warn(String.format("Free disk space below threshold. Available: %d bytes (threshold: %s)",
67-
diskFreeInBytes, this.threshold));
68-
}
66+
logger.warn(LogMessage.format("Free disk space below threshold. Available: %d bytes (threshold: %s)",
67+
diskFreeInBytes, this.threshold));
6968
builder.down();
7069
}
7170
builder.withDetail("total", this.path.getTotalSpace()).withDetail("free", diskFreeInBytes)

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ static class Neo4jWebConfiguration {
111111

112112
@Bean
113113
OpenSessionInViewInterceptor neo4jOpenSessionInViewInterceptor(Neo4jProperties properties) {
114-
if (properties.getOpenInView() == null && logger.isWarnEnabled()) {
114+
if (properties.getOpenInView() == null) {
115115
logger.warn("spring.data.neo4j.open-in-view is enabled by default."
116116
+ "Therefore, database queries may be performed during view "
117117
+ "rendering. Explicitly configure "

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import org.springframework.context.annotation.Bean;
4242
import org.springframework.context.annotation.Configuration;
4343
import org.springframework.context.i18n.LocaleContextHolder;
44+
import org.springframework.core.log.LogMessage;
4445
import org.springframework.web.servlet.view.UrlBasedViewResolver;
4546
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfig;
4647
import org.springframework.web.servlet.view.groovy.GroovyMarkupConfigurer;
@@ -83,10 +84,11 @@ public GroovyMarkupConfiguration(ApplicationContext applicationContext, GroovyTe
8384
public void checkTemplateLocationExists() {
8485
if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) {
8586
TemplateLocation location = new TemplateLocation(this.properties.getResourceLoaderPath());
86-
if (!location.exists(this.applicationContext) && logger.isWarnEnabled()) {
87-
logger.warn("Cannot find template location: " + location
88-
+ " (please add some templates, check your Groovy "
89-
+ "configuration, or set spring.groovy.template.check-template-location=false)");
87+
if (!location.exists(this.applicationContext)) {
88+
logger.warn(LogMessage.format(
89+
"Cannot find template location: %s (please add some templates, check your Groovy "
90+
+ "configuration, or set spring.groovy.template.check-template-location=false)",
91+
location));
9092
}
9193
}
9294
}

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,8 @@ static class JodaDateTimeJacksonConfiguration {
119119

120120
@Bean
121121
SimpleModule jodaDateTimeSerializationModule(JacksonProperties jacksonProperties) {
122-
if (logger.isWarnEnabled()) {
123-
logger.warn("Auto-configuration of Jackson's Joda-Time integration is deprecated in favor of using "
124-
+ "java.time (JSR-310).");
125-
}
122+
logger.warn("Auto-configuration of Jackson's Joda-Time integration is deprecated in favor of using "
123+
+ "java.time (JSR-310).");
126124
SimpleModule module = new SimpleModule();
127125
JacksonJodaDateFormat jacksonJodaFormat = getJacksonJodaDateFormat(jacksonProperties);
128126
if (jacksonJodaFormat != null) {

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerInvoker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.beans.factory.ObjectProvider;
2626
import org.springframework.context.ApplicationContext;
2727
import org.springframework.context.ApplicationListener;
28+
import org.springframework.core.log.LogMessage;
2829

2930
/**
3031
* Bean to handle {@link DataSource} initialization by running {@literal schema-*.sql} on
@@ -76,9 +77,8 @@ private void initialize(DataSourceInitializer initializer) {
7677
}
7778
}
7879
catch (IllegalStateException ex) {
79-
if (logger.isWarnEnabled()) {
80-
logger.warn("Could not send event to complete DataSource initialization (" + ex.getMessage() + ")");
81-
}
80+
logger.warn(LogMessage.format("Could not send event to complete DataSource initialization (%s)",
81+
ex.getMessage()));
8282
}
8383
}
8484

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ protected JpaWebConfiguration(JpaProperties jpaProperties) {
216216

217217
@Bean
218218
public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() {
219-
if (this.jpaProperties.getOpenInView() == null && logger.isWarnEnabled()) {
219+
if (this.jpaProperties.getOpenInView() == null) {
220220
logger.warn("spring.jpa.open-in-view is enabled by default. "
221221
+ "Therefore, database queries may be performed during view "
222222
+ "rendering. Explicitly configure spring.jpa.open-in-view to disable this warning");

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
import org.springframework.context.ApplicationContext;
3535
import org.springframework.core.NestedExceptionUtils;
3636
import org.springframework.core.io.Resource;
37+
import org.springframework.core.log.LogMessage;
3738
import org.springframework.http.HttpLogging;
3839
import org.springframework.http.HttpStatus;
3940
import org.springframework.http.codec.HttpMessageReader;
@@ -287,9 +288,9 @@ protected void logError(ServerRequest request, ServerResponse response, Throwabl
287288
logger.debug(request.exchange().getLogPrefix() + formatError(throwable, request));
288289
}
289290
if (HttpStatus.resolve(response.rawStatusCode()) != null
290-
&& response.statusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR) && logger.isErrorEnabled()) {
291-
logger.error(request.exchange().getLogPrefix() + "500 Server Error for " + formatRequest(request),
292-
throwable);
291+
&& response.statusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
292+
logger.error(LogMessage.of(() -> String.format("%s 500 Server Error for %s",
293+
request.exchange().getLogPrefix(), formatRequest(request))), throwable);
293294
}
294295
}
295296

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/OptionalLiveReloadServer.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.springframework.beans.factory.InitializingBean;
2323
import org.springframework.boot.devtools.livereload.LiveReloadServer;
24+
import org.springframework.core.log.LogMessage;
2425

2526
/**
2627
* Manages an optional {@link LiveReloadServer}. The {@link LiveReloadServer} may
@@ -54,9 +55,7 @@ void startServer() throws Exception {
5455
if (!this.server.isStarted()) {
5556
this.server.start();
5657
}
57-
if (logger.isInfoEnabled()) {
58-
logger.info("LiveReload server is running on port " + this.server.getPort());
59-
}
58+
logger.info(LogMessage.format("LiveReload server is running on port %s", this.server.getPort()));
6059
}
6160
catch (Exception ex) {
6261
logger.warn("Unable to start LiveReload server");

spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RemoteDevToolsAutoConfiguration.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.springframework.context.annotation.Conditional;
4949
import org.springframework.context.annotation.Configuration;
5050
import org.springframework.context.annotation.Import;
51+
import org.springframework.core.log.LogMessage;
5152
import org.springframework.http.server.ServerHttpRequest;
5253

5354
/**
@@ -126,9 +127,7 @@ UrlHandlerMapper remoteRestartHandlerMapper(HttpRestartServer server, ServerProp
126127
RemoteDevToolsProperties remote = properties.getRemote();
127128
String servletContextPath = (servlet.getContextPath() != null) ? servlet.getContextPath() : "";
128129
String url = servletContextPath + remote.getContextPath() + "/restart";
129-
if (logger.isWarnEnabled()) {
130-
logger.warn("Listening for remote restart updates on " + url);
131-
}
130+
logger.warn(LogMessage.format("Listening for remote restart updates on %s", url));
132131
Handler handler = new HttpRestartServerHandler(server);
133132
return new UrlHandlerMapper(url, handler);
134133
}

0 commit comments

Comments
 (0)