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;
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());
}
/**
......
......@@ -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;
}
}
......@@ -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()));
}
......
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