Skip to content

Commit 240b1f9

Browse files
wycmphilwebb
authored andcommitted
Optimize logger calls
Guard logger calls to ensure that they are only made when the level is set. See gh-18710
1 parent 744dcd9 commit 240b1f9

File tree

27 files changed

+92
-44
lines changed

27 files changed

+92
-44
lines changed

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ private void bindMetricsRegistryToHikariDataSource(HikariDataSource hikari) {
124124
hikari.setMetricsTrackerFactory(new MicrometerMetricsTrackerFactory(this.registry));
125125
}
126126
catch (Exception ex) {
127-
logger.warn("Failed to bind Hikari metrics: " + ex.getMessage());
127+
if (logger.isWarnEnabled()) {
128+
logger.warn("Failed to bind Hikari metrics: " + ex.getMessage());
129+
}
128130
}
129131
}
130132
}

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,10 @@ protected void doHealthCheck(Health.Builder builder) throws Exception {
6262
builder.up();
6363
}
6464
else {
65-
logger.warn(String.format("Free disk space below threshold. Available: %d bytes (threshold: %s)",
66-
diskFreeInBytes, this.threshold));
65+
if (logger.isWarnEnabled()) {
66+
logger.warn(String.format("Free disk space below threshold. Available: %d bytes (threshold: %s)",
67+
diskFreeInBytes, this.threshold));
68+
}
6769
builder.down();
6870
}
6971
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) {
114+
if (properties.getOpenInView() == null && logger.isWarnEnabled()) {
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public GroovyMarkupConfiguration(ApplicationContext applicationContext, GroovyTe
8383
public void checkTemplateLocationExists() {
8484
if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) {
8585
TemplateLocation location = new TemplateLocation(this.properties.getResourceLoaderPath());
86-
if (!location.exists(this.applicationContext)) {
86+
if (!location.exists(this.applicationContext) && logger.isWarnEnabled()) {
8787
logger.warn("Cannot find template location: " + location
8888
+ " (please add some templates, check your Groovy "
8989
+ "configuration, or set spring.groovy.template.check-template-location=false)");

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

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

120120
@Bean
121121
SimpleModule jodaDateTimeSerializationModule(JacksonProperties jacksonProperties) {
122-
logger.warn("Auto-configuration of Jackson's Joda-Time integration is deprecated in favor of using "
123-
+ "java.time (JSR-310).");
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+
}
124126
SimpleModule module = new SimpleModule();
125127
JacksonJodaDateFormat jacksonJodaFormat = getJacksonJodaDateFormat(jacksonProperties);
126128
if (jacksonJodaFormat != null) {

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,9 @@ private void initialize(DataSourceInitializer initializer) {
7676
}
7777
}
7878
catch (IllegalStateException ex) {
79-
logger.warn("Could not send event to complete DataSource initialization (" + ex.getMessage() + ")");
79+
if (logger.isWarnEnabled()) {
80+
logger.warn("Could not send event to complete DataSource initialization (" + ex.getMessage() + ")");
81+
}
8082
}
8183
}
8284

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ public MustacheAutoConfiguration(MustacheProperties mustache, ApplicationContext
6363
public void checkTemplateLocationExists() {
6464
if (this.mustache.isCheckTemplateLocation()) {
6565
TemplateLocation location = new TemplateLocation(this.mustache.getPrefix());
66-
if (!location.exists(this.applicationContext)) {
66+
if (!location.exists(this.applicationContext) && logger.isWarnEnabled()) {
6767
logger.warn("Cannot find template location: " + location
6868
+ " (please add some templates, check your Mustache configuration, or set spring.mustache."
6969
+ "check-template-location=false)");

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) {
219+
if (this.jpaProperties.getOpenInView() == null && logger.isWarnEnabled()) {
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: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ protected void logError(ServerRequest request, ServerResponse response, Throwabl
287287
logger.debug(request.exchange().getLogPrefix() + formatError(throwable, request));
288288
}
289289
if (HttpStatus.resolve(response.rawStatusCode()) != null
290-
&& response.statusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR)) {
290+
&& response.statusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR) && logger.isErrorEnabled()) {
291291
logger.error(request.exchange().getLogPrefix() + "500 Server Error for " + formatRequest(request),
292292
throwable);
293293
}

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,9 @@ void startServer() throws Exception {
5454
if (!this.server.isStarted()) {
5555
this.server.start();
5656
}
57-
logger.info("LiveReload server is running on port " + this.server.getPort());
57+
if (logger.isInfoEnabled()) {
58+
logger.info("LiveReload server is running on port " + this.server.getPort());
59+
}
5860
}
5961
catch (Exception ex) {
6062
logger.warn("Unable to start LiveReload server");

0 commit comments

Comments
 (0)