From 5eed35eb676981e6f519fcdcdd856ca59545c157 Mon Sep 17 00:00:00 2001 From: wycm Date: Wed, 23 Oct 2019 20:27:06 +0800 Subject: [PATCH 1/3] Optimization log --- .../DataSourcePoolMetricsAutoConfiguration.java | 4 +++- .../actuate/system/DiskSpaceHealthIndicator.java | 6 ++++-- .../data/neo4j/Neo4jDataAutoConfiguration.java | 2 +- .../template/GroovyTemplateAutoConfiguration.java | 2 +- .../jackson/JacksonAutoConfiguration.java | 6 ++++-- .../jdbc/DataSourceInitializerInvoker.java | 4 +++- .../mustache/MustacheAutoConfiguration.java | 2 +- .../orm/jpa/JpaBaseConfiguration.java | 2 +- .../error/AbstractErrorWebExceptionHandler.java | 2 +- .../autoconfigure/OptionalLiveReloadServer.java | 4 +++- .../RemoteDevToolsAutoConfiguration.java | 4 +++- .../boot/devtools/classpath/ClassPathFolders.java | 8 ++++++-- .../env/DevToolsPropertyDefaultsPostProcessor.java | 6 ++++-- .../remote/client/ClassPathChangeUploader.java | 12 ++++++++---- .../remote/client/RemoteClientConfiguration.java | 2 +- .../boot/devtools/restart/ChangeableUrls.java | 2 +- .../restart/RestartApplicationListener.java | 4 +++- .../tunnel/client/HttpTunnelConnection.java | 6 ++++-- .../boot/devtools/tunnel/client/TunnelClient.java | 8 ++++++-- .../server/SocketTargetServerConnection.java | 4 +++- .../java/org/springframework/boot/ImageBanner.java | 6 ++++-- .../org/springframework/boot/ResourceBanner.java | 8 +++++--- .../springframework/boot/StartupInfoLogger.java | 4 +++- .../context/FileEncodingApplicationListener.java | 14 ++++++++------ .../logging/LoggingApplicationListener.java | 4 +++- .../boot/diagnostics/FailureAnalyzers.java | 4 +++- .../boot/web/context/WebServerPortFileWriter.java | 4 +++- 27 files changed, 90 insertions(+), 44 deletions(-) diff --git a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/jdbc/DataSourcePoolMetricsAutoConfiguration.java b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/jdbc/DataSourcePoolMetricsAutoConfiguration.java index 2ee47d82ea77..25970b4016bc 100644 --- a/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/jdbc/DataSourcePoolMetricsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-actuator-autoconfigure/src/main/java/org/springframework/boot/actuate/autoconfigure/metrics/jdbc/DataSourcePoolMetricsAutoConfiguration.java @@ -124,7 +124,9 @@ private void bindMetricsRegistryToHikariDataSource(HikariDataSource hikari) { hikari.setMetricsTrackerFactory(new MicrometerMetricsTrackerFactory(this.registry)); } catch (Exception ex) { - logger.warn("Failed to bind Hikari metrics: " + ex.getMessage()); + if (logger.isWarnEnabled()) { + logger.warn("Failed to bind Hikari metrics: " + ex.getMessage()); + } } } } diff --git a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java index 46f44f2b48f2..c0719d7e5f93 100644 --- a/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java +++ b/spring-boot-project/spring-boot-actuator/src/main/java/org/springframework/boot/actuate/system/DiskSpaceHealthIndicator.java @@ -62,8 +62,10 @@ protected void doHealthCheck(Health.Builder builder) throws Exception { builder.up(); } else { - logger.warn(String.format("Free disk space below threshold. Available: %d bytes (threshold: %s)", - diskFreeInBytes, this.threshold)); + if (logger.isWarnEnabled()) { + logger.warn(String.format("Free disk space below threshold. Available: %d bytes (threshold: %s)", + diskFreeInBytes, this.threshold)); + } builder.down(); } builder.withDetail("total", this.path.getTotalSpace()).withDetail("free", diskFreeInBytes) diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java index 585efa568252..1d3ad3b3efa8 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/data/neo4j/Neo4jDataAutoConfiguration.java @@ -111,7 +111,7 @@ static class Neo4jWebConfiguration { @Bean OpenSessionInViewInterceptor neo4jOpenSessionInViewInterceptor(Neo4jProperties properties) { - if (properties.getOpenInView() == null) { + if (properties.getOpenInView() == null && logger.isWarnEnabled()) { logger.warn("spring.data.neo4j.open-in-view is enabled by default." + "Therefore, database queries may be performed during view " + "rendering. Explicitly configure " diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java index 0bad6a82765e..79fedf6644c1 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/groovy/template/GroovyTemplateAutoConfiguration.java @@ -83,7 +83,7 @@ public GroovyMarkupConfiguration(ApplicationContext applicationContext, GroovyTe public void checkTemplateLocationExists() { if (this.properties.isCheckTemplateLocation() && !isUsingGroovyAllJar()) { TemplateLocation location = new TemplateLocation(this.properties.getResourceLoaderPath()); - if (!location.exists(this.applicationContext)) { + if (!location.exists(this.applicationContext) && logger.isWarnEnabled()) { logger.warn("Cannot find template location: " + location + " (please add some templates, check your Groovy " + "configuration, or set spring.groovy.template.check-template-location=false)"); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java index 79caab4a3eb7..adcb7baa113c 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jackson/JacksonAutoConfiguration.java @@ -119,8 +119,10 @@ static class JodaDateTimeJacksonConfiguration { @Bean SimpleModule jodaDateTimeSerializationModule(JacksonProperties jacksonProperties) { - logger.warn("Auto-configuration of Jackson's Joda-Time integration is deprecated in favor of using " - + "java.time (JSR-310)."); + if (logger.isWarnEnabled()) { + logger.warn("Auto-configuration of Jackson's Joda-Time integration is deprecated in favor of using " + + "java.time (JSR-310)."); + } SimpleModule module = new SimpleModule(); JacksonJodaDateFormat jacksonJodaFormat = getJacksonJodaDateFormat(jacksonProperties); if (jacksonJodaFormat != null) { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerInvoker.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerInvoker.java index d9891f035312..6b38496c86dc 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerInvoker.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/jdbc/DataSourceInitializerInvoker.java @@ -76,7 +76,9 @@ private void initialize(DataSourceInitializer initializer) { } } catch (IllegalStateException ex) { - logger.warn("Could not send event to complete DataSource initialization (" + ex.getMessage() + ")"); + if (logger.isWarnEnabled()) { + logger.warn("Could not send event to complete DataSource initialization (" + ex.getMessage() + ")"); + } } } diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java index ac8bcbc1c6c3..227b41280bf5 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/mustache/MustacheAutoConfiguration.java @@ -63,7 +63,7 @@ public MustacheAutoConfiguration(MustacheProperties mustache, ApplicationContext public void checkTemplateLocationExists() { if (this.mustache.isCheckTemplateLocation()) { TemplateLocation location = new TemplateLocation(this.mustache.getPrefix()); - if (!location.exists(this.applicationContext)) { + if (!location.exists(this.applicationContext) && logger.isWarnEnabled()) { logger.warn("Cannot find template location: " + location + " (please add some templates, check your Mustache configuration, or set spring.mustache." + "check-template-location=false)"); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java index 4f9400ea31e2..7039c5f3ef1b 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/orm/jpa/JpaBaseConfiguration.java @@ -216,7 +216,7 @@ protected JpaWebConfiguration(JpaProperties jpaProperties) { @Bean public OpenEntityManagerInViewInterceptor openEntityManagerInViewInterceptor() { - if (this.jpaProperties.getOpenInView() == null) { + if (this.jpaProperties.getOpenInView() == null && logger.isWarnEnabled()) { logger.warn("spring.jpa.open-in-view is enabled by default. " + "Therefore, database queries may be performed during view " + "rendering. Explicitly configure spring.jpa.open-in-view to disable this warning"); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java index 812929801e61..74caef1fedcf 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/reactive/error/AbstractErrorWebExceptionHandler.java @@ -287,7 +287,7 @@ protected void logError(ServerRequest request, ServerResponse response, Throwabl logger.debug(request.exchange().getLogPrefix() + formatError(throwable, request)); } if (HttpStatus.resolve(response.rawStatusCode()) != null - && response.statusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR)) { + && response.statusCode().equals(HttpStatus.INTERNAL_SERVER_ERROR) && logger.isErrorEnabled()) { logger.error(request.exchange().getLogPrefix() + "500 Server Error for " + formatRequest(request), throwable); } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/OptionalLiveReloadServer.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/OptionalLiveReloadServer.java index a6363fe7677b..e31ded7e15dc 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/OptionalLiveReloadServer.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/OptionalLiveReloadServer.java @@ -54,7 +54,9 @@ void startServer() throws Exception { if (!this.server.isStarted()) { this.server.start(); } - logger.info("LiveReload server is running on port " + this.server.getPort()); + if (logger.isInfoEnabled()) { + logger.info("LiveReload server is running on port " + this.server.getPort()); + } } catch (Exception ex) { logger.warn("Unable to start LiveReload server"); diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RemoteDevToolsAutoConfiguration.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RemoteDevToolsAutoConfiguration.java index 3d1111bec8be..fd961f92a0a3 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RemoteDevToolsAutoConfiguration.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/autoconfigure/RemoteDevToolsAutoConfiguration.java @@ -126,7 +126,9 @@ UrlHandlerMapper remoteRestartHandlerMapper(HttpRestartServer server, ServerProp RemoteDevToolsProperties remote = properties.getRemote(); String servletContextPath = (servlet.getContextPath() != null) ? servlet.getContextPath() : ""; String url = servletContextPath + remote.getContextPath() + "/restart"; - logger.warn("Listening for remote restart updates on " + url); + if (logger.isWarnEnabled()) { + logger.warn("Listening for remote restart updates on " + url); + } Handler handler = new HttpRestartServerHandler(server); return new UrlHandlerMapper(url, handler); } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/classpath/ClassPathFolders.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/classpath/ClassPathFolders.java index 0934568235ae..71c64182a7ff 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/classpath/ClassPathFolders.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/classpath/ClassPathFolders.java @@ -58,8 +58,12 @@ private void addUrl(URL url) { this.folders.add(ResourceUtils.getFile(url)); } catch (Exception ex) { - logger.warn("Unable to get classpath URL " + url); - logger.trace("Unable to get classpath URL " + url, ex); + if (logger.isWarnEnabled()) { + logger.warn("Unable to get classpath URL " + url); + } + if (logger.isTraceEnabled()) { + logger.trace("Unable to get classpath URL " + url, ex); + } } } } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java index 9d45de8f133d..d29eb778cbb5 100755 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/env/DevToolsPropertyDefaultsPostProcessor.java @@ -80,10 +80,12 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) { if (DevToolsEnablementDeducer.shouldEnable(Thread.currentThread()) && isLocalApplication(environment)) { if (canAddProperties(environment)) { - logger.info("Devtools property defaults active! Set '" + ENABLED + "' to 'false' to disable"); + if (logger.isInfoEnabled()) { + logger.info("Devtools property defaults active! Set '" + ENABLED + "' to 'false' to disable"); + } environment.getPropertySources().addLast(new MapPropertySource("devtools", PROPERTIES)); } - if (isWebApplication(environment) && !environment.containsProperty(WEB_LOGGING)) { + if (isWebApplication(environment) && !environment.containsProperty(WEB_LOGGING) && logger.isInfoEnabled()) { logger.info("For additional web related logging consider setting the '" + WEB_LOGGING + "' property to 'DEBUG'"); } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/ClassPathChangeUploader.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/ClassPathChangeUploader.java index fd2d8c27d595..26628facbc05 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/ClassPathChangeUploader.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/ClassPathChangeUploader.java @@ -114,8 +114,10 @@ private void performUpload(ClassLoaderFiles classLoaderFiles, byte[] bytes) thro return; } catch (SocketException ex) { - logger.warn("A failure occurred when uploading to " + this.uri - + ". Upload will be retried in 2 seconds"); + if (logger.isWarnEnabled()) { + logger.warn("A failure occurred when uploading to " + this.uri + + ". Upload will be retried in 2 seconds"); + } logger.debug("Upload failure", ex); Thread.sleep(2000); } @@ -128,8 +130,10 @@ private void performUpload(ClassLoaderFiles classLoaderFiles, byte[] bytes) thro } private void logUpload(ClassLoaderFiles classLoaderFiles) { - int size = classLoaderFiles.size(); - logger.info("Uploaded " + size + " class " + ((size != 1) ? "resources" : "resource")); + if (logger.isInfoEnabled()) { + int size = classLoaderFiles.size(); + logger.info("Uploaded " + size + " class " + ((size != 1) ? "resources" : "resource")); + } } private byte[] serialize(ClassLoaderFiles classLoaderFiles) throws IOException { diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/RemoteClientConfiguration.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/RemoteClientConfiguration.java index 9073ea444fa6..2b508d506186 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/RemoteClientConfiguration.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/remote/client/RemoteClientConfiguration.java @@ -118,7 +118,7 @@ private void logWarnings() { if (!remoteProperties.getRestart().isEnabled()) { logger.warn("Remote restart is disabled."); } - if (!this.remoteUrl.startsWith("https://")) { + if (!this.remoteUrl.startsWith("https://") && logger.isWarnEnabled()) { logger.warn("The connection to " + this.remoteUrl + " is insecure. You should use a URL starting with 'https://'."); } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java index 723e79087653..d8cfe8bad60f 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/ChangeableUrls.java @@ -170,7 +170,7 @@ private static List getUrlsFromManifestClassPathAttribute(URL jarUrl, JarFi throw new IllegalStateException("Class-Path attribute contains malformed URL", ex); } } - if (!nonExistentEntries.isEmpty()) { + if (!nonExistentEntries.isEmpty() && logger.isInfoEnabled()) { logger.info("The Class-Path manifest attribute in " + jarFile.getName() + " referenced one or more files that do not exist: " + StringUtils.collectionToCommaDelimitedString(nonExistentEntries)); diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java index c4c4e15379ba..5a5663e68204 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/restart/RestartApplicationListener.java @@ -73,7 +73,9 @@ private void onApplicationStartingEvent(ApplicationStartingEvent event) { Restarter.initialize(args, false, initializer, restartOnInitialize); } else { - logger.info("Restart disabled due to System property '" + ENABLED_PROPERTY + "' being set to false"); + if (logger.isInfoEnabled()) { + logger.info("Restart disabled due to System property '" + ENABLED_PROPERTY + "' being set to false"); + } Restarter.disable(); } } diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java index d17c06860acb..e1d188469e22 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java @@ -92,7 +92,9 @@ protected HttpTunnelConnection(String url, ClientHttpRequestFactory requestFacto @Override public TunnelChannel open(WritableByteChannel incomingChannel, Closeable closeable) throws Exception { - logger.trace("Opening HTTP tunnel to " + this.uri); + if (logger.isTraceEnabled()) { + logger.trace("Opening HTTP tunnel to " + this.uri); + } return new TunnelChannel(incomingChannel, closeable); } @@ -151,7 +153,7 @@ public void run() { sendAndReceive(payload); } catch (IOException ex) { - if (ex instanceof ConnectException) { + if (ex instanceof ConnectException && logger.isWarnEnabled()) { logger.warn("Failed to connect to remote application at " + HttpTunnelConnection.this.uri); } else { diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/TunnelClient.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/TunnelClient.java index 0f340e3c53b0..2580aa24f6f5 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/TunnelClient.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/TunnelClient.java @@ -88,7 +88,9 @@ public int start() throws IOException { ServerSocketChannel serverSocketChannel = ServerSocketChannel.open(); serverSocketChannel.socket().bind(new InetSocketAddress(this.listenPort)); int port = serverSocketChannel.socket().getLocalPort(); - logger.trace("Listening for TCP traffic to tunnel on port " + port); + if (logger.isTraceEnabled()) { + logger.trace("Listening for TCP traffic to tunnel on port " + port); + } this.serverThread = new ServerThread(serverSocketChannel); this.serverThread.start(); return port; @@ -144,7 +146,9 @@ public ServerThread(ServerSocketChannel serverSocketChannel) { } public void close() throws IOException { - logger.trace("Closing tunnel client on port " + this.serverSocketChannel.socket().getLocalPort()); + if (logger.isTraceEnabled()) { + logger.trace("Closing tunnel client on port " + this.serverSocketChannel.socket().getLocalPort()); + } this.serverSocketChannel.close(); this.acceptConnections = false; interrupt(); diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/server/SocketTargetServerConnection.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/server/SocketTargetServerConnection.java index c70ffe362b2e..b499986efffd 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/server/SocketTargetServerConnection.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/server/SocketTargetServerConnection.java @@ -54,7 +54,9 @@ public SocketTargetServerConnection(PortProvider portProvider) { @Override public ByteChannel open(int socketTimeout) throws IOException { SocketAddress address = new InetSocketAddress(this.portProvider.getPort()); - logger.trace("Opening tunnel connection to target server on " + address); + if (logger.isTraceEnabled()) { + logger.trace("Opening tunnel connection to target server on " + address); + } SocketChannel channel = SocketChannel.open(address); channel.socket().setSoTimeout(socketTimeout); return new TimeoutAwareChannel(channel); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java index de4591ec5834..7445f0e788f1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ImageBanner.java @@ -78,8 +78,10 @@ public void printBanner(Environment environment, Class sourceClass, PrintStre printBanner(environment, out); } catch (Throwable ex) { - logger.warn("Image banner not printable: " + this.image + " (" + ex.getClass() + ": '" + ex.getMessage() - + "')"); + if (logger.isWarnEnabled()) { + logger.warn("Image banner not printable: " + this.image + " (" + ex.getClass() + ": '" + ex.getMessage() + + "')"); + } logger.debug("Image banner printing failure", ex); } finally { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java index d2df72e256aa..bd8b66383255 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java @@ -70,9 +70,11 @@ public void printBanner(Environment environment, Class sourceClass, PrintStre out.println(banner); } catch (Exception ex) { - logger.warn( - "Banner not printable: " + this.resource + " (" + ex.getClass() + ": '" + ex.getMessage() + "')", - ex); + if (logger.isWarnEnabled()) { + logger.warn( + "Banner not printable: " + this.resource + " (" + ex.getClass() + ": '" + ex.getMessage() + "')", + ex); + } } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java index 630807b526da..f05aec620ac1 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java @@ -121,7 +121,9 @@ private void appendOn(StringBuilder message) { if (System.getProperty("os.name").toLowerCase().contains("mac")) { warning.append(" (macOS machines may need to add entries to /etc/hosts)"); } - logger.warn(warning.append(".")); + if (logger.isWarnEnabled()) { + logger.warn(warning.append(".")); + } } } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/FileEncodingApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/FileEncodingApplicationListener.java index 661c03c2a987..9cbb667aec1b 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/FileEncodingApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/FileEncodingApplicationListener.java @@ -64,12 +64,14 @@ public void onApplicationEvent(ApplicationEnvironmentPreparedEvent event) { String encoding = System.getProperty("file.encoding"); String desired = environment.getProperty("spring.mandatory-file-encoding"); if (encoding != null && !desired.equalsIgnoreCase(encoding)) { - logger.error("System property 'file.encoding' is currently '" + encoding + "'. It should be '" + desired - + "' (as defined in 'spring.mandatoryFileEncoding')."); - logger.error("Environment variable LANG is '" + System.getenv("LANG") - + "'. You could use a locale setting that matches encoding='" + desired + "'."); - logger.error("Environment variable LC_ALL is '" + System.getenv("LC_ALL") - + "'. You could use a locale setting that matches encoding='" + desired + "'."); + if (logger.isErrorEnabled()) { + logger.error("System property 'file.encoding' is currently '" + encoding + "'. It should be '" + desired + + "' (as defined in 'spring.mandatoryFileEncoding')."); + logger.error("Environment variable LANG is '" + System.getenv("LANG") + + "'. You could use a locale setting that matches encoding='" + desired + "'."); + logger.error("Environment variable LC_ALL is '" + System.getenv("LC_ALL") + + "'. You could use a locale setting that matches encoding='" + desired + "'."); + } throw new IllegalStateException("The Java Virtual Machine has not been configured to use the " + "desired default character encoding (" + desired + ")."); } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java index 9ff891beccfc..af5e927c2614 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/context/logging/LoggingApplicationListener.java @@ -416,7 +416,9 @@ private BiConsumer getLogLevelConfigurer(LoggingSystem system) system.setLogLevel(name, level); } catch (RuntimeException ex) { - this.logger.error("Cannot set level '" + level + "' for '" + name + "'"); + if (this.logger.isErrorEnabled()) { + this.logger.error("Cannot set level '" + level + "' for '" + name + "'"); + } } }; } diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java index 5f4e4b233ec6..4ff6420aa6a7 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/diagnostics/FailureAnalyzers.java @@ -76,7 +76,9 @@ private List loadFailureAnalyzers(ClassLoader classLoader) { analyzers.add((FailureAnalyzer) constructor.newInstance()); } catch (Throwable ex) { - logger.trace("Failed to load " + analyzerName, ex); + if (logger.isTraceEnabled()) { + logger.trace("Failed to load " + analyzerName, ex); + } } } AnnotationAwareOrderComparator.sort(analyzers); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java index 14b1c5297128..6974867e56fb 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/context/WebServerPortFileWriter.java @@ -91,7 +91,9 @@ public void onApplicationEvent(WebServerInitializedEvent event) { portFile.deleteOnExit(); } catch (Exception ex) { - logger.warn(String.format("Cannot create port file %s", this.file)); + if (logger.isWarnEnabled()) { + logger.warn(String.format("Cannot create port file %s", this.file)); + } } } From 8a4c5d976408621a3b974491f21d68e72d82e2ef Mon Sep 17 00:00:00 2001 From: wycm Date: Wed, 23 Oct 2019 20:40:41 +0800 Subject: [PATCH 2/3] Optimization log --- .../boot/devtools/tunnel/client/HttpTunnelConnection.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java index e1d188469e22..bf8e8f5aa66c 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java @@ -153,8 +153,10 @@ public void run() { sendAndReceive(payload); } catch (IOException ex) { - if (ex instanceof ConnectException && logger.isWarnEnabled()) { - logger.warn("Failed to connect to remote application at " + HttpTunnelConnection.this.uri); + if (ex instanceof ConnectException) { + if (logger.isWarnEnabled()) { + logger.warn("Failed to connect to remote application at " + HttpTunnelConnection.this.uri); + } } else { logger.trace("Unexpected connection error", ex); From f78f8bfd459e9acea2a3a2bca1e1558cc1607ea2 Mon Sep 17 00:00:00 2001 From: wycm Date: Thu, 24 Oct 2019 10:17:11 +0800 Subject: [PATCH 3/3] Apply spring-javaformat --- .../boot/devtools/tunnel/client/HttpTunnelConnection.java | 3 ++- .../main/java/org/springframework/boot/ResourceBanner.java | 5 ++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java index bf8e8f5aa66c..770ae093aa6f 100644 --- a/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java +++ b/spring-boot-project/spring-boot-devtools/src/main/java/org/springframework/boot/devtools/tunnel/client/HttpTunnelConnection.java @@ -155,7 +155,8 @@ public void run() { catch (IOException ex) { if (ex instanceof ConnectException) { if (logger.isWarnEnabled()) { - logger.warn("Failed to connect to remote application at " + HttpTunnelConnection.this.uri); + logger.warn( + "Failed to connect to remote application at " + HttpTunnelConnection.this.uri); } } else { diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java index bd8b66383255..694489dda5e2 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/ResourceBanner.java @@ -71,9 +71,8 @@ public void printBanner(Environment environment, Class sourceClass, PrintStre } catch (Exception ex) { if (logger.isWarnEnabled()) { - logger.warn( - "Banner not printable: " + this.resource + " (" + ex.getClass() + ": '" + ex.getMessage() + "')", - ex); + logger.warn("Banner not printable: " + this.resource + " (" + ex.getClass() + ": '" + ex.getMessage() + + "')", ex); } } }