Improve context-related logging on web startup

Sample output at TRACE:
```
DispatcherServlet - Initializing Servlet 'org.springframework.web.servlet.DispatcherServlet-7a8c8dcf'
AnnotationConfigWebApplicationContext - Refreshing WebApplicationContext for namespace 'org.springframework.web.servlet.DispatcherServlet-7a8c8dcf-servlet', started on Wed Jul 25 17:46:38 EDT 2018
AnnotationConfigWebApplicationContext - Registering [org.springframework.web.servlet.mvc.method.annotation.RequestPartIntegrationTests$CommonsMultipartResolverTestConfig]
AnnotationConfigWebApplicationContext - No 'messageSource' bean, using [Empty MessageSource]
AnnotationConfigWebApplicationContext - No 'applicationEventMulticaster' bean, using [SimpleApplicationEventMulticaster]
AnnotationConfigWebApplicationContext - No 'lifecycleProcessor' bean, using [DefaultLifecycleProcessor]
...
DispatcherServlet - Initialization completed in 3361 ms
```

Issue: SPR-16946
This commit is contained in:
Rossen Stoyanchev 2018-07-25 17:50:03 -04:00
parent 4e03d3fdcb
commit 14d0fee86c
6 changed files with 25 additions and 45 deletions

View File

@ -585,7 +585,12 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
this.active.set(true); this.active.set(true);
if (logger.isDebugEnabled()) { if (logger.isDebugEnabled()) {
logger.debug("Refreshing " + this); if (logger.isTraceEnabled()) {
logger.trace("Refreshing " + this);
}
else {
logger.debug("Refreshing " + getDisplayName());
}
} }
// Initialize any placeholder property sources in the context environment // Initialize any placeholder property sources in the context environment
@ -732,8 +737,7 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
this.messageSource = dms; this.messageSource = dms;
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource); beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME + logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
"': using default [" + this.messageSource + "]");
} }
} }
} }
@ -756,9 +760,8 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory); this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster); beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Unable to locate ApplicationEventMulticaster with name '" + logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
"': using default [" + this.applicationEventMulticaster + "]");
} }
} }
} }
@ -783,9 +786,8 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
this.lifecycleProcessor = defaultProcessor; this.lifecycleProcessor = defaultProcessor;
beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor); beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Unable to locate LifecycleProcessor with name '" + logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " +
LIFECYCLE_PROCESSOR_BEAN_NAME + "[" + this.lifecycleProcessor.getClass().getSimpleName() + "]");
"': using default [" + this.lifecycleProcessor + "]");
} }
} }
} }
@ -1384,14 +1386,10 @@ public abstract class AbstractApplicationContext extends DefaultResourceLoader
@Override @Override
public String toString() { public String toString() {
StringBuilder sb = new StringBuilder(getDisplayName()); StringBuilder sb = new StringBuilder(getDisplayName());
sb.append(": startup date [").append(new Date(getStartupDate())); sb.append(", started on ").append(new Date(getStartupDate()));
sb.append("]; ");
ApplicationContext parent = getParent(); ApplicationContext parent = getParent();
if (parent == null) { if (parent != null) {
sb.append("root of context hierarchy"); sb.append(", parent: ").append(parent.getDisplayName());
}
else {
sb.append("parent: ").append(parent.getDisplayName());
} }
return sb.toString(); return sb.toString();
} }

View File

@ -92,4 +92,10 @@ public class DelegatingMessageSource extends MessageSourceSupport implements Hie
} }
} }
@Override
public String toString() {
return this.parentMessageSource != null ? this.parentMessageSource.toString() : "Empty MessageSource";
}
} }

View File

@ -303,7 +303,7 @@ public class ContextLoader {
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime; long elapsedTime = System.currentTimeMillis() - startTime;
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms"); logger.info("Root WebApplicationContext initialized in " + elapsedTime + " ms");
} }
return this.context; return this.context;

View File

@ -230,7 +230,7 @@ public class AnnotationConfigWebApplicationContext extends AbstractRefreshableWe
try { try {
Class<?> clazz = ClassUtils.forName(configLocation, getClassLoader()); Class<?> clazz = ClassUtils.forName(configLocation, getClassLoader());
if (logger.isTraceEnabled()) { if (logger.isTraceEnabled()) {
logger.trace("Successfully resolved class for [" + configLocation + "]"); logger.trace("Registering [" + configLocation + "]");
} }
reader.register(clazz); reader.register(clazz);
} }

View File

@ -516,9 +516,9 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
*/ */
@Override @Override
protected final void initServletBean() throws ServletException { protected final void initServletBean() throws ServletException {
getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'"); getServletContext().log("Initializing Spring " + getClass().getSimpleName() + " '" + getServletName() + "'");
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
logger.info("FrameworkServlet '" + getServletName() + "': initialization started"); logger.info("Initializing Servlet '" + getServletName() + "'");
} }
long startTime = System.currentTimeMillis(); long startTime = System.currentTimeMillis();
@ -540,9 +540,7 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
} }
if (logger.isInfoEnabled()) { if (logger.isInfoEnabled()) {
long elapsedTime = System.currentTimeMillis() - startTime; logger.info("Completed initialization in " + (System.currentTimeMillis() - startTime) + " ms");
logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
elapsedTime + " ms");
} }
} }
@ -600,10 +598,6 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
// Publish the context as a servlet context attribute. // Publish the context as a servlet context attribute.
String attrName = getServletContextAttributeName(); String attrName = getServletContextAttributeName();
getServletContext().setAttribute(attrName, wac); getServletContext().setAttribute(attrName, wac);
if (logger.isTraceEnabled()) {
logger.trace("Published WebApplicationContext of servlet '" + getServletName() +
"' as ServletContext attribute [" + attrName + "]");
}
} }
return wac; return wac;
@ -650,11 +644,6 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
*/ */
protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) { protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {
Class<?> contextClass = getContextClass(); Class<?> contextClass = getContextClass();
if (logger.isTraceEnabled()) {
logger.trace("Servlet '" + getServletName() +
"' will create custom WebApplicationContext context of class '" +
contextClass.getName() + "'" + ", parent context [" + parent + "]");
}
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) { if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
throw new ApplicationContextException( throw new ApplicationContextException(
"Fatal initialization error in servlet with name '" + getServletName() + "Fatal initialization error in servlet with name '" + getServletName() +
@ -1071,9 +1060,6 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
if (requestAttributes != null) { if (requestAttributes != null) {
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable); RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
} }
if (logger.isTraceEnabled()) {
logger.trace("Bound request context to thread: " + request);
}
} }
private void resetContextHolders(HttpServletRequest request, private void resetContextHolders(HttpServletRequest request,
@ -1081,9 +1067,6 @@ public abstract class FrameworkServlet extends HttpServletBean implements Applic
LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable); LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable);
RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable); RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable);
if (logger.isTraceEnabled()) {
logger.trace("Cleared thread-bound request context: " + request);
}
} }
private void logResult(HttpServletRequest request, HttpServletResponse response, private void logResult(HttpServletRequest request, HttpServletResponse response,

View File

@ -146,9 +146,6 @@ public abstract class HttpServletBean extends HttpServlet implements Environment
*/ */
@Override @Override
public final void init() throws ServletException { public final void init() throws ServletException {
if (logger.isTraceEnabled()) {
logger.trace("Initializing servlet '" + getServletName() + "'");
}
// Set bean properties from init parameters. // Set bean properties from init parameters.
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties); PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
@ -170,10 +167,6 @@ public abstract class HttpServletBean extends HttpServlet implements Environment
// Let subclasses do whatever initialization they like. // Let subclasses do whatever initialization they like.
initServletBean(); initServletBean();
if (logger.isTraceEnabled()) {
logger.trace("Servlet '" + getServletName() + "' configured successfully");
}
} }
/** /**