From 57442f5b1274540335507c9de0d42cbbc34b7821 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 19 Dec 2014 14:45:57 -0800 Subject: [PATCH] Improve formatting of Tomcat port(s) logging Align Tomcat logging with the recent Jetty and Undertow updates. --- .../TomcatEmbeddedServletContainer.java | 45 ++++++++----------- 1 file changed, 18 insertions(+), 27 deletions(-) diff --git a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java index 3244243540..624695bcc2 100644 --- a/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java +++ b/spring-boot/src/main/java/org/springframework/boot/context/embedded/tomcat/TomcatEmbeddedServletContainer.java @@ -44,7 +44,8 @@ import org.springframework.util.Assert; */ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer { - private final Log logger = LogFactory.getLog(TomcatEmbeddedServletContainer.class); + private static final Log logger = LogFactory + .getLog(TomcatEmbeddedServletContainer.class); private static AtomicInteger containerCounter = new AtomicInteger(-1); @@ -75,8 +76,8 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer } private synchronized void initialize() throws EmbeddedServletContainerException { - this.logger.info("Tomcat initialized with port(s): " - + getConfiguredPortsDescription()); + TomcatEmbeddedServletContainer.logger.info("Tomcat initialized with port(s): " + + getPortsDescription(false)); try { addInstanceIdToEngineName(); @@ -99,15 +100,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer } } - private String getConfiguredPortsDescription() { - StringBuilder ports = new StringBuilder(); - for (Connector connector : this.tomcat.getService().findConnectors()) { - ports.append(ports.length() == 0 ? "" : " "); - ports.append(connector.getPort() + "/" + connector.getScheme()); - } - return ports.toString(); - } - private void addInstanceIdToEngineName() { int instanceId = containerCounter.incrementAndGet(); if (instanceId > 0) { @@ -157,15 +149,13 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer if (connector != null && this.autoStart) { startConnector(connector); } - // Ensure process isn't left running if it actually failed to start - if (connectorsHaveFailedToStart()) { stopSilently(); throw new IllegalStateException("Tomcat connector in failed state"); } - - this.logger.info("Tomcat started on port(s): " + getActualPortsDescription()); + TomcatEmbeddedServletContainer.logger.info("Tomcat started on port(s): " + + getPortsDescription(true)); } private boolean connectorsHaveFailedToStart() { @@ -206,7 +196,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer connector.getProtocolHandler().stop(); } catch (Exception ex) { - this.logger.error("Cannot pause connector: ", ex); + TomcatEmbeddedServletContainer.logger.error("Cannot pause connector: ", ex); } } @@ -219,7 +209,7 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer } } catch (Exception ex) { - this.logger.error("Cannot start connector: ", ex); + TomcatEmbeddedServletContainer.logger.error("Cannot start connector: ", ex); throw new EmbeddedServletContainerException( "Unable to start embedded Tomcat connectors", ex); } @@ -229,15 +219,6 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer return this.serviceConnectors; } - private String getActualPortsDescription() { - StringBuilder ports = new StringBuilder(); - for (Connector connector : this.tomcat.getService().findConnectors()) { - ports.append(ports.length() == 0 ? "" : " "); - ports.append(connector.getLocalPort() + "/" + connector.getScheme()); - } - return ports.toString(); - } - @Override public synchronized void stop() throws EmbeddedServletContainerException { try { @@ -258,6 +239,16 @@ public class TomcatEmbeddedServletContainer implements EmbeddedServletContainer } } + private String getPortsDescription(boolean localPort) { + StringBuilder ports = new StringBuilder(); + for (Connector connector : this.tomcat.getService().findConnectors()) { + ports.append(ports.length() == 0 ? "" : " "); + int port = (localPort ? connector.getLocalPort() : connector.getPort()); + ports.append(port + " (" + connector.getScheme() + ")"); + } + return ports.toString(); + } + @Override public int getPort() { Connector connector = this.tomcat.getConnector();