Commit 6edc7570 authored by Phillip Webb's avatar Phillip Webb

Added 'Application started in XX seconds' logging

Added logging to INFO SpringApplication to log when the application has
fully started and how long it took to load.
parent cbb95e3e
...@@ -51,6 +51,7 @@ import org.springframework.core.io.ResourceLoader; ...@@ -51,6 +51,7 @@ import org.springframework.core.io.ResourceLoader;
import org.springframework.core.io.support.SpringFactoriesLoader; import org.springframework.core.io.support.SpringFactoriesLoader;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
import org.springframework.web.context.ConfigurableWebApplicationContext; import org.springframework.web.context.ConfigurableWebApplicationContext;
import org.springframework.web.context.support.StandardServletEnvironment; import org.springframework.web.context.support.StandardServletEnvironment;
...@@ -245,6 +246,9 @@ public class SpringApplication { ...@@ -245,6 +246,9 @@ public class SpringApplication {
* @return a running {@link ApplicationContext} * @return a running {@link ApplicationContext}
*/ */
public ApplicationContext run(String... args) { public ApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// Call all non environment aware initializers very early // Call all non environment aware initializers very early
callNonEnvironmentAwareSpringApplicationInitializers(args); callNonEnvironmentAwareSpringApplicationInitializers(args);
...@@ -275,6 +279,13 @@ public class SpringApplication { ...@@ -275,6 +279,13 @@ public class SpringApplication {
} }
load(context, sources.toArray(new Object[sources.size()])); load(context, sources.toArray(new Object[sources.size()]));
refresh(context); refresh(context);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass).logStarted(
getApplicationLog(), stopWatch);
}
runCommandLineRunners(context, args); runCommandLineRunners(context, args);
return context; return context;
} }
...@@ -363,9 +374,12 @@ public class SpringApplication { ...@@ -363,9 +374,12 @@ public class SpringApplication {
} }
} }
/**
* Called to log startup information, subclasses may override to add additional
* logging.
*/
protected void logStartupInfo() { protected void logStartupInfo() {
Log applicationLog = getApplicationLog(); new StartupInfoLogger(this.mainApplicationClass).logStarting(getApplicationLog());
new StartupInfoLogger(this.mainApplicationClass).log(applicationLog);
} }
/** /**
......
...@@ -28,6 +28,7 @@ import org.apache.commons.logging.Log; ...@@ -28,6 +28,7 @@ import org.apache.commons.logging.Log;
import org.springframework.context.ApplicationContext; import org.springframework.context.ApplicationContext;
import org.springframework.util.Assert; import org.springframework.util.Assert;
import org.springframework.util.ClassUtils; import org.springframework.util.ClassUtils;
import org.springframework.util.StopWatch;
import org.springframework.util.StringUtils; import org.springframework.util.StringUtils;
/** /**
...@@ -44,7 +45,7 @@ class StartupInfoLogger { ...@@ -44,7 +45,7 @@ class StartupInfoLogger {
this.sourceClass = sourceClass; this.sourceClass = sourceClass;
} }
public void log(Log log) { public void logStarting(Log log) {
Assert.notNull(log, "Log must not be null"); Assert.notNull(log, "Log must not be null");
if (log.isInfoEnabled()) { if (log.isInfoEnabled()) {
log.info(getStartupMessage()); log.info(getStartupMessage());
...@@ -54,6 +55,12 @@ class StartupInfoLogger { ...@@ -54,6 +55,12 @@ class StartupInfoLogger {
} }
} }
public void logStarted(Log log, StopWatch stopWatch) {
if (log.isInfoEnabled()) {
log.info(getStartedMessage(stopWatch));
}
}
private String getStartupMessage() { private String getStartupMessage() {
StringBuilder message = new StringBuilder(); StringBuilder message = new StringBuilder();
message.append("Starting "); message.append("Starting ");
...@@ -74,6 +81,16 @@ class StartupInfoLogger { ...@@ -74,6 +81,16 @@ class StartupInfoLogger {
return message; 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() { private String getApplicationName() {
return (this.sourceClass != null ? ClassUtils.getShortName(this.sourceClass) return (this.sourceClass != null ? ClassUtils.getShortName(this.sourceClass)
: "application"); : "application");
...@@ -163,4 +180,5 @@ class StartupInfoLogger { ...@@ -163,4 +180,5 @@ class StartupInfoLogger {
} }
return defaultValue; return defaultValue;
} }
} }
...@@ -39,7 +39,7 @@ public class StartUpLoggerTests { ...@@ -39,7 +39,7 @@ public class StartUpLoggerTests {
@Test @Test
public void sourceClassIncluded() { public void sourceClassIncluded() {
new StartupInfoLogger(getClass()).log(this.log); new StartupInfoLogger(getClass()).logStarting(this.log);
assertTrue("Wrong output: " + this.output, assertTrue("Wrong output: " + this.output,
this.output.toString().contains("Starting " + getClass().getSimpleName())); this.output.toString().contains("Starting " + getClass().getSimpleName()));
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment