diff --git a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java index 38ecef34ba..7600288dfe 100644 --- a/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java +++ b/spring-boot/src/main/java/org/springframework/boot/SpringApplication.java @@ -51,6 +51,7 @@ import org.springframework.core.io.ResourceLoader; import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.support.StandardServletEnvironment; @@ -245,6 +246,9 @@ public class SpringApplication { * @return a running {@link ApplicationContext} */ public ApplicationContext run(String... args) { + StopWatch stopWatch = new StopWatch(); + stopWatch.start(); + // Call all non environment aware initializers very early callNonEnvironmentAwareSpringApplicationInitializers(args); @@ -275,6 +279,13 @@ public class SpringApplication { } load(context, sources.toArray(new Object[sources.size()])); refresh(context); + + stopWatch.stop(); + if (this.logStartupInfo) { + new StartupInfoLogger(this.mainApplicationClass).logStarted( + getApplicationLog(), stopWatch); + } + runCommandLineRunners(context, args); return context; } @@ -363,9 +374,12 @@ public class SpringApplication { } } + /** + * Called to log startup information, subclasses may override to add additional + * logging. + */ protected void logStartupInfo() { - Log applicationLog = getApplicationLog(); - new StartupInfoLogger(this.mainApplicationClass).log(applicationLog); + new StartupInfoLogger(this.mainApplicationClass).logStarting(getApplicationLog()); } /** diff --git a/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java b/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java index 312117e936..a30bb9ab0f 100644 --- a/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java +++ b/spring-boot/src/main/java/org/springframework/boot/StartupInfoLogger.java @@ -28,6 +28,7 @@ import org.apache.commons.logging.Log; import org.springframework.context.ApplicationContext; import org.springframework.util.Assert; import org.springframework.util.ClassUtils; +import org.springframework.util.StopWatch; import org.springframework.util.StringUtils; /** @@ -44,7 +45,7 @@ class StartupInfoLogger { this.sourceClass = sourceClass; } - public void log(Log log) { + public void logStarting(Log log) { Assert.notNull(log, "Log must not be null"); if (log.isInfoEnabled()) { log.info(getStartupMessage()); @@ -54,6 +55,12 @@ class StartupInfoLogger { } } + public void logStarted(Log log, StopWatch stopWatch) { + if (log.isInfoEnabled()) { + log.info(getStartedMessage(stopWatch)); + } + } + private String getStartupMessage() { StringBuilder message = new StringBuilder(); message.append("Starting "); @@ -74,6 +81,16 @@ class StartupInfoLogger { return message; } + private StringBuilder getStartedMessage(StopWatch stopWatch) { + StringBuilder message = new StringBuilder(); + message.append("Started "); + message.append(getApplicationName()); + message.append(" in "); + message.append(stopWatch.getTotalTimeSeconds()); + message.append(" seconds"); + return message; + } + private String getApplicationName() { return (this.sourceClass != null ? ClassUtils.getShortName(this.sourceClass) : "application"); @@ -163,4 +180,5 @@ class StartupInfoLogger { } return defaultValue; } + } diff --git a/spring-boot/src/test/java/org/springframework/boot/StartUpLoggerTests.java b/spring-boot/src/test/java/org/springframework/boot/StartUpLoggerTests.java index 19e5d53d9c..aa98d246e1 100644 --- a/spring-boot/src/test/java/org/springframework/boot/StartUpLoggerTests.java +++ b/spring-boot/src/test/java/org/springframework/boot/StartUpLoggerTests.java @@ -39,7 +39,7 @@ public class StartUpLoggerTests { @Test public void sourceClassIncluded() { - new StartupInfoLogger(getClass()).log(this.log); + new StartupInfoLogger(getClass()).logStarting(this.log); assertTrue("Wrong output: " + this.output, this.output.toString().contains("Starting " + getClass().getSimpleName())); }