No longer log hostname during application start

Remove hostname logging since `InetAddress.getLocalHost().getHostName()`
causes a network lookup and can take a long time.

Closes gh-32908
pull/32943/head
Phillip Webb 2 years ago
parent c81ebf63d9
commit 2b5a29d514

@ -21,7 +21,6 @@ import java.io.IOException;
import java.nio.file.Files; import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
@ -134,10 +133,8 @@ public class ApplicationRunner extends DefaultTask {
private void awaitLogging(Process process) { private void awaitLogging(Process process) {
long end = System.currentTimeMillis() + 30000; long end = System.currentTimeMillis() + 30000;
String expectedLogging = this.expectedLogging.get(); String expectedLogging = this.expectedLogging.get();
List<String> outputLines = Collections.emptyList();
while (System.currentTimeMillis() < end) { while (System.currentTimeMillis() < end) {
outputLines = outputLines(); for (String line : outputLines()) {
for (String line : outputLines) {
if (line.contains(expectedLogging)) { if (line.contains(expectedLogging)) {
return; return;
} }
@ -146,10 +143,7 @@ public class ApplicationRunner extends DefaultTask {
throw new IllegalStateException("Process exited before '" + expectedLogging + "' was logged"); throw new IllegalStateException("Process exited before '" + expectedLogging + "' was logged");
} }
} }
StringBuilder message = new StringBuilder( throw new IllegalStateException("'" + expectedLogging + "' was not logged within 30 seconds");
"After 30 seconds '" + expectedLogging + "' had not be logged in the following output:\n\n");
outputLines.forEach((line) -> message.append(line).append("\n"));
throw new IllegalStateException(message.toString());
} }
private List<String> outputLines() { private List<String> outputLines() {
@ -177,8 +171,8 @@ public class ApplicationRunner extends DefaultTask {
private List<String> normalize(List<String> lines) { private List<String> normalize(List<String> lines) {
List<String> normalizedLines = lines; List<String> normalizedLines = lines;
Map<String, String> normalizations = new HashMap<>(this.normalizations); Map<String, String> normalizations = new HashMap<>(this.normalizations);
normalizations.put("(Starting .* using Java .* on ).*( with PID [\\d]+ \\().*( started by ).*( in ).*(\\))", normalizations.put("(Starting .* using Java .* with PID [\\d]+ \\().*( started by ).*( in ).*(\\))",
"$1myhost$2" + this.applicationJar.get() + "$3myuser$4/opt/apps/$5"); "$1" + this.applicationJar.get() + "$2myuser$3/opt/apps/$4");
for (Entry<String, String> normalization : normalizations.entrySet()) { for (Entry<String, String> normalization : normalizations.entrySet()) {
Pattern pattern = Pattern.compile(normalization.getKey()); Pattern pattern = Pattern.compile(normalization.getKey());
normalizedLines = normalize(normalizedLines, pattern, normalization.getValue()); normalizedLines = normalize(normalizedLines, pattern, normalization.getValue());

@ -6,7 +6,7 @@
=========|_|==============|___/=/_/_/_/ =========|_|==============|___/=/_/_/_/
:: Spring Boot :: :: Spring Boot ::
2017-08-08 17:12:30.910 INFO 19866 --- [ main] s.f.SampleWebFreeMarkerApplication : Starting SampleWebFreeMarkerApplication on host.local with PID 19866 2017-08-08 17:12:30.910 INFO 19866 --- [ main] s.f.SampleWebFreeMarkerApplication : Starting SampleWebFreeMarkerApplication with PID 19866
2017-08-08 17:12:30.913 INFO 19866 --- [ main] s.f.SampleWebFreeMarkerApplication : No active profile set, falling back to default profiles: default 2017-08-08 17:12:30.913 INFO 19866 --- [ main] s.f.SampleWebFreeMarkerApplication : No active profile set, falling back to default profiles: default
2017-08-08 17:12:30.952 INFO 19866 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@76b10754: startup date [Tue Aug 08 17:12:30 BST 2017]; root of context hierarchy 2017-08-08 17:12:30.952 INFO 19866 --- [ main] ConfigServletWebServerApplicationContext : Refreshing org.springframework.boot.web.servlet.context.AnnotationConfigServletWebServerApplicationContext@76b10754: startup date [Tue Aug 08 17:12:30 BST 2017]; root of context hierarchy
2017-08-08 17:12:31.878 INFO 19866 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2017-08-08 17:12:31.878 INFO 19866 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)

@ -17,12 +17,10 @@
package org.springframework.boot; package org.springframework.boot;
import java.lang.management.ManagementFactory; import java.lang.management.ManagementFactory;
import java.net.InetAddress;
import java.time.Duration; import java.time.Duration;
import java.util.concurrent.Callable; import java.util.concurrent.Callable;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.aot.AotDetector; import org.springframework.aot.AotDetector;
import org.springframework.boot.system.ApplicationHome; import org.springframework.boot.system.ApplicationHome;
@ -42,10 +40,6 @@ import org.springframework.util.StringUtils;
*/ */
class StartupInfoLogger { class StartupInfoLogger {
private static final Log logger = LogFactory.getLog(StartupInfoLogger.class);
private static final long HOST_NAME_RESOLVE_THRESHOLD = 200;
private final Class<?> sourceClass; private final Class<?> sourceClass;
StartupInfoLogger(Class<?> sourceClass) { StartupInfoLogger(Class<?> sourceClass) {
@ -71,7 +65,6 @@ class StartupInfoLogger {
appendApplicationName(message); appendApplicationName(message);
appendVersion(message, this.sourceClass); appendVersion(message, this.sourceClass);
appendJavaVersion(message); appendJavaVersion(message);
appendOn(message);
appendPid(message); appendPid(message);
appendContext(message); appendContext(message);
return message; return message;
@ -116,26 +109,6 @@ class StartupInfoLogger {
append(message, "v", () -> source.getPackage().getImplementationVersion()); append(message, "v", () -> source.getPackage().getImplementationVersion());
} }
private void appendOn(StringBuilder message) {
long startTime = System.currentTimeMillis();
append(message, "on ", () -> InetAddress.getLocalHost().getHostName());
long resolveTime = System.currentTimeMillis() - startTime;
if (resolveTime > HOST_NAME_RESOLVE_THRESHOLD) {
logger.warn(LogMessage.of(() -> {
StringBuilder warning = new StringBuilder();
warning.append("InetAddress.getLocalHost().getHostName() took ");
warning.append(resolveTime);
warning.append(" milliseconds to respond.");
warning.append(" Please verify your network configuration");
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
warning.append(" (macOS machines may need to add entries to /etc/hosts)");
}
warning.append(".");
return warning;
}));
}
}
private void appendPid(StringBuilder message) { private void appendPid(StringBuilder message) {
append(message, "with PID ", ApplicationPid::new); append(message, "with PID ", ApplicationPid::new);
} }

@ -16,8 +16,6 @@
package org.springframework.boot; package org.springframework.boot;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.time.Duration; import java.time.Duration;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
@ -43,29 +41,28 @@ class StartupInfoLoggerTests {
private final Log log = mock(Log.class); private final Log log = mock(Log.class);
@Test @Test
void startingFormat() throws UnknownHostException { void startingFormat() {
given(this.log.isInfoEnabled()).willReturn(true); given(this.log.isInfoEnabled()).willReturn(true);
new StartupInfoLogger(getClass()).logStarting(this.log); new StartupInfoLogger(getClass()).logStarting(this.log);
ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
then(this.log).should().info(captor.capture()); then(this.log).should().info(captor.capture());
assertThat(captor.getValue().toString()).contains("Starting " + getClass().getSimpleName() + " using Java " assertThat(captor.getValue().toString()).contains("Starting " + getClass().getSimpleName() + " using Java "
+ System.getProperty("java.version") + " on " + InetAddress.getLocalHost().getHostName() + " with PID " + System.getProperty("java.version") + " with PID " + new ApplicationPid() + " (started by "
+ new ApplicationPid() + " (started by " + System.getProperty("user.name") + " in " + System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")");
+ System.getProperty("user.dir") + ")");
} }
@Test @Test
void startingFormatInAotMode() throws UnknownHostException { void startingFormatInAotMode() {
System.setProperty("spring.aot.enabled", "true"); System.setProperty("spring.aot.enabled", "true");
try { try {
given(this.log.isInfoEnabled()).willReturn(true); given(this.log.isInfoEnabled()).willReturn(true);
new StartupInfoLogger(getClass()).logStarting(this.log); new StartupInfoLogger(getClass()).logStarting(this.log);
ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class); ArgumentCaptor<Object> captor = ArgumentCaptor.forClass(Object.class);
then(this.log).should().info(captor.capture()); then(this.log).should().info(captor.capture());
assertThat(captor.getValue().toString()).contains("Starting AOT-processed " + getClass().getSimpleName() assertThat(captor.getValue().toString())
+ " using Java " + System.getProperty("java.version") + " on " .contains("Starting AOT-processed " + getClass().getSimpleName() + " using Java "
+ InetAddress.getLocalHost().getHostName() + " with PID " + new ApplicationPid() + " (started by " + System.getProperty("java.version") + " with PID " + new ApplicationPid() + " (started by "
+ System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")"); + System.getProperty("user.name") + " in " + System.getProperty("user.dir") + ")");
} }
finally { finally {

Loading…
Cancel
Save