Polish "Optimize logger calls"

See gh-18710
This commit is contained in:
Phillip Webb
2019-10-23 20:44:52 -07:00
parent 240b1f9e29
commit 597baf9774
25 changed files with 87 additions and 103 deletions

View File

@@ -21,6 +21,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.boot.devtools.livereload.LiveReloadServer;
import org.springframework.core.log.LogMessage;
/**
* Manages an optional {@link LiveReloadServer}. The {@link LiveReloadServer} may
@@ -54,9 +55,7 @@ public class OptionalLiveReloadServer implements InitializingBean {
if (!this.server.isStarted()) {
this.server.start();
}
if (logger.isInfoEnabled()) {
logger.info("LiveReload server is running on port " + this.server.getPort());
}
logger.info(LogMessage.format("LiveReload server is running on port %s", this.server.getPort()));
}
catch (Exception ex) {
logger.warn("Unable to start LiveReload server");

View File

@@ -48,6 +48,7 @@ import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Conditional;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.core.log.LogMessage;
import org.springframework.http.server.ServerHttpRequest;
/**
@@ -126,9 +127,7 @@ public class RemoteDevToolsAutoConfiguration {
RemoteDevToolsProperties remote = properties.getRemote();
String servletContextPath = (servlet.getContextPath() != null) ? servlet.getContextPath() : "";
String url = servletContextPath + remote.getContextPath() + "/restart";
if (logger.isWarnEnabled()) {
logger.warn("Listening for remote restart updates on " + url);
}
logger.warn(LogMessage.format("Listening for remote restart updates on %s", url));
Handler handler = new HttpRestartServerHandler(server);
return new UrlHandlerMapper(url, handler);
}

View File

@@ -26,6 +26,7 @@ import java.util.List;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.log.LogMessage;
import org.springframework.util.ResourceUtils;
/**
@@ -58,12 +59,8 @@ public class ClassPathFolders implements Iterable<File> {
this.folders.add(ResourceUtils.getFile(url));
}
catch (Exception ex) {
if (logger.isWarnEnabled()) {
logger.warn("Unable to get classpath URL " + url);
}
if (logger.isTraceEnabled()) {
logger.trace("Unable to get classpath URL " + url, ex);
}
logger.warn(LogMessage.format("Unable to get classpath URL %s", url));
logger.trace(LogMessage.format("Unable to get classpath URL ", url), ex);
}
}
}

View File

@@ -32,6 +32,7 @@ import org.springframework.core.annotation.Order;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import org.springframework.core.log.LogMessage;
import org.springframework.util.ClassUtils;
/**
@@ -80,14 +81,14 @@ public class DevToolsPropertyDefaultsPostProcessor implements EnvironmentPostPro
public void postProcessEnvironment(ConfigurableEnvironment environment, SpringApplication application) {
if (DevToolsEnablementDeducer.shouldEnable(Thread.currentThread()) && isLocalApplication(environment)) {
if (canAddProperties(environment)) {
if (logger.isInfoEnabled()) {
logger.info("Devtools property defaults active! Set '" + ENABLED + "' to 'false' to disable");
}
logger.info(LogMessage.format("Devtools property defaults active! Set '%s' to 'false' to disable",
ENABLED));
environment.getPropertySources().addLast(new MapPropertySource("devtools", PROPERTIES));
}
if (isWebApplication(environment) && !environment.containsProperty(WEB_LOGGING) && logger.isInfoEnabled()) {
logger.info("For additional web related logging consider setting the '" + WEB_LOGGING
+ "' property to 'DEBUG'");
if (isWebApplication(environment) && !environment.containsProperty(WEB_LOGGING)) {
logger.info(LogMessage.format(
"For additional web related logging consider setting the '%s' property to 'DEBUG'",
WEB_LOGGING));
}
}
}

View File

@@ -38,6 +38,7 @@ import org.springframework.boot.devtools.restart.classloader.ClassLoaderFile;
import org.springframework.boot.devtools.restart.classloader.ClassLoaderFile.Kind;
import org.springframework.boot.devtools.restart.classloader.ClassLoaderFiles;
import org.springframework.context.ApplicationListener;
import org.springframework.core.log.LogMessage;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
@@ -114,10 +115,8 @@ public class ClassPathChangeUploader implements ApplicationListener<ClassPathCha
return;
}
catch (SocketException ex) {
if (logger.isWarnEnabled()) {
logger.warn("A failure occurred when uploading to " + this.uri
+ ". Upload will be retried in 2 seconds");
}
logger.warn(LogMessage.format(
"A failure occurred when uploading to %s. Upload will be retried in 2 seconds", this.uri));
logger.debug("Upload failure", ex);
Thread.sleep(2000);
}
@@ -130,10 +129,8 @@ public class ClassPathChangeUploader implements ApplicationListener<ClassPathCha
}
private void logUpload(ClassLoaderFiles classLoaderFiles) {
if (logger.isInfoEnabled()) {
int size = classLoaderFiles.size();
logger.info("Uploaded " + size + " class " + ((size != 1) ? "resources" : "resource"));
}
int size = classLoaderFiles.size();
logger.info(LogMessage.format("Uploaded %s class %s", size, (size != 1) ? "resources" : "resource"));
}
private byte[] serialize(ClassLoaderFiles classLoaderFiles) throws IOException {

View File

@@ -53,6 +53,7 @@ import org.springframework.context.ApplicationListener;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.core.log.LogMessage;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.springframework.http.client.InterceptingClientHttpRequestFactory;
@@ -118,9 +119,10 @@ public class RemoteClientConfiguration implements InitializingBean {
if (!remoteProperties.getRestart().isEnabled()) {
logger.warn("Remote restart is disabled.");
}
if (!this.remoteUrl.startsWith("https://") && logger.isWarnEnabled()) {
logger.warn("The connection to " + this.remoteUrl
+ " is insecure. You should use a URL starting with 'https://'.");
if (!this.remoteUrl.startsWith("https://")) {
logger.warn(LogMessage.format(
"The connection to %s is insecure. You should use a URL starting with 'https://'.",
this.remoteUrl));
}
}

View File

@@ -37,6 +37,7 @@ import org.apache.commons.logging.Log;
import org.springframework.boot.devtools.logger.DevToolsLogFactory;
import org.springframework.boot.devtools.settings.DevToolsSettings;
import org.springframework.core.log.LogMessage;
import org.springframework.util.StringUtils;
/**
@@ -170,10 +171,10 @@ final class ChangeableUrls implements Iterable<URL> {
throw new IllegalStateException("Class-Path attribute contains malformed URL", ex);
}
}
if (!nonExistentEntries.isEmpty() && logger.isInfoEnabled()) {
logger.info("The Class-Path manifest attribute in " + jarFile.getName()
if (!nonExistentEntries.isEmpty()) {
logger.info(LogMessage.of(() -> "The Class-Path manifest attribute in " + jarFile.getName()
+ " referenced one or more files that do not exist: "
+ StringUtils.collectionToCommaDelimitedString(nonExistentEntries));
+ StringUtils.collectionToCommaDelimitedString(nonExistentEntries)));
}
return urls;
}

View File

@@ -26,6 +26,7 @@ import org.springframework.boot.context.event.ApplicationStartingEvent;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationListener;
import org.springframework.core.Ordered;
import org.springframework.core.log.LogMessage;
/**
* {@link ApplicationListener} to initialize the {@link Restarter}.
@@ -73,9 +74,8 @@ public class RestartApplicationListener implements ApplicationListener<Applicati
Restarter.initialize(args, false, initializer, restartOnInitialize);
}
else {
if (logger.isInfoEnabled()) {
logger.info("Restart disabled due to System property '" + ENABLED_PROPERTY + "' being set to false");
}
logger.info(LogMessage.format("Restart disabled due to System property '%s' being set to false",
ENABLED_PROPERTY));
Restarter.disable();
}
}

View File

@@ -35,6 +35,7 @@ import org.apache.commons.logging.LogFactory;
import org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayload;
import org.springframework.boot.devtools.tunnel.payload.HttpTunnelPayloadForwarder;
import org.springframework.core.log.LogMessage;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.client.ClientHttpRequest;
@@ -92,9 +93,7 @@ public class HttpTunnelConnection implements TunnelConnection {
@Override
public TunnelChannel open(WritableByteChannel incomingChannel, Closeable closeable) throws Exception {
if (logger.isTraceEnabled()) {
logger.trace("Opening HTTP tunnel to " + this.uri);
}
logger.trace(LogMessage.format("Opening HTTP tunnel to %s", this.uri));
return new TunnelChannel(incomingChannel, closeable);
}
@@ -154,10 +153,8 @@ public class HttpTunnelConnection implements TunnelConnection {
}
catch (IOException ex) {
if (ex instanceof ConnectException) {
if (logger.isWarnEnabled()) {
logger.warn(
"Failed to connect to remote application at " + HttpTunnelConnection.this.uri);
}
logger.warn(LogMessage.format("Failed to connect to remote application at %s",
HttpTunnelConnection.this.uri));
}
else {
logger.trace("Unexpected connection error", ex);

View File

@@ -30,6 +30,7 @@ import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.SmartInitializingSingleton;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
/**
@@ -88,9 +89,7 @@ public class TunnelClient implements SmartInitializingSingleton {
ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
serverSocketChannel.socket().bind(new InetSocketAddress(this.listenPort));
int port = serverSocketChannel.socket().getLocalPort();
if (logger.isTraceEnabled()) {
logger.trace("Listening for TCP traffic to tunnel on port " + port);
}
logger.trace(LogMessage.format("Listening for TCP traffic to tunnel on port %s", port));
this.serverThread = new ServerThread(serverSocketChannel);
this.serverThread.start();
return port;
@@ -146,9 +145,8 @@ public class TunnelClient implements SmartInitializingSingleton {
}
public void close() throws IOException {
if (logger.isTraceEnabled()) {
logger.trace("Closing tunnel client on port " + this.serverSocketChannel.socket().getLocalPort());
}
logger.trace(LogMessage.format("Closing tunnel client on port %s",
this.serverSocketChannel.socket().getLocalPort()));
this.serverSocketChannel.close();
this.acceptConnections = false;
interrupt();

View File

@@ -28,6 +28,7 @@ import java.nio.channels.SocketChannel;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.core.log.LogMessage;
import org.springframework.util.Assert;
/**
@@ -54,9 +55,7 @@ public class SocketTargetServerConnection implements TargetServerConnection {
@Override
public ByteChannel open(int socketTimeout) throws IOException {
SocketAddress address = new InetSocketAddress(this.portProvider.getPort());
if (logger.isTraceEnabled()) {
logger.trace("Opening tunnel connection to target server on " + address);
}
logger.trace(LogMessage.format("Opening tunnel connection to target server on %s", address));
SocketChannel channel = SocketChannel.open(address);
channel.socket().setSoTimeout(socketTimeout);
return new TimeoutAwareChannel(channel);