Skip to content

Commit 14d0fee

Browse files
committed
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
1 parent 4e03d3f commit 14d0fee

File tree

6 files changed

+25
-45
lines changed

6 files changed

+25
-45
lines changed

spring-context/src/main/java/org/springframework/context/support/AbstractApplicationContext.java

+14-16
Original file line numberDiff line numberDiff line change
@@ -585,7 +585,12 @@ protected void prepareRefresh() {
585585
this.active.set(true);
586586

587587
if (logger.isDebugEnabled()) {
588-
logger.debug("Refreshing " + this);
588+
if (logger.isTraceEnabled()) {
589+
logger.trace("Refreshing " + this);
590+
}
591+
else {
592+
logger.debug("Refreshing " + getDisplayName());
593+
}
589594
}
590595

591596
// Initialize any placeholder property sources in the context environment
@@ -732,8 +737,7 @@ protected void initMessageSource() {
732737
this.messageSource = dms;
733738
beanFactory.registerSingleton(MESSAGE_SOURCE_BEAN_NAME, this.messageSource);
734739
if (logger.isTraceEnabled()) {
735-
logger.trace("Unable to locate MessageSource with name '" + MESSAGE_SOURCE_BEAN_NAME +
736-
"': using default [" + this.messageSource + "]");
740+
logger.trace("No '" + MESSAGE_SOURCE_BEAN_NAME + "' bean, using [" + this.messageSource + "]");
737741
}
738742
}
739743
}
@@ -756,9 +760,8 @@ protected void initApplicationEventMulticaster() {
756760
this.applicationEventMulticaster = new SimpleApplicationEventMulticaster(beanFactory);
757761
beanFactory.registerSingleton(APPLICATION_EVENT_MULTICASTER_BEAN_NAME, this.applicationEventMulticaster);
758762
if (logger.isTraceEnabled()) {
759-
logger.trace("Unable to locate ApplicationEventMulticaster with name '" +
760-
APPLICATION_EVENT_MULTICASTER_BEAN_NAME +
761-
"': using default [" + this.applicationEventMulticaster + "]");
763+
logger.trace("No '" + APPLICATION_EVENT_MULTICASTER_BEAN_NAME + "' bean, using " +
764+
"[" + this.applicationEventMulticaster.getClass().getSimpleName() + "]");
762765
}
763766
}
764767
}
@@ -783,9 +786,8 @@ protected void initLifecycleProcessor() {
783786
this.lifecycleProcessor = defaultProcessor;
784787
beanFactory.registerSingleton(LIFECYCLE_PROCESSOR_BEAN_NAME, this.lifecycleProcessor);
785788
if (logger.isTraceEnabled()) {
786-
logger.trace("Unable to locate LifecycleProcessor with name '" +
787-
LIFECYCLE_PROCESSOR_BEAN_NAME +
788-
"': using default [" + this.lifecycleProcessor + "]");
789+
logger.trace("No '" + LIFECYCLE_PROCESSOR_BEAN_NAME + "' bean, using " +
790+
"[" + this.lifecycleProcessor.getClass().getSimpleName() + "]");
789791
}
790792
}
791793
}
@@ -1384,14 +1386,10 @@ public boolean isRunning() {
13841386
@Override
13851387
public String toString() {
13861388
StringBuilder sb = new StringBuilder(getDisplayName());
1387-
sb.append(": startup date [").append(new Date(getStartupDate()));
1388-
sb.append("]; ");
1389+
sb.append(", started on ").append(new Date(getStartupDate()));
13891390
ApplicationContext parent = getParent();
1390-
if (parent == null) {
1391-
sb.append("root of context hierarchy");
1392-
}
1393-
else {
1394-
sb.append("parent: ").append(parent.getDisplayName());
1391+
if (parent != null) {
1392+
sb.append(", parent: ").append(parent.getDisplayName());
13951393
}
13961394
return sb.toString();
13971395
}

spring-context/src/main/java/org/springframework/context/support/DelegatingMessageSource.java

+6
Original file line numberDiff line numberDiff line change
@@ -92,4 +92,10 @@ public String getMessage(MessageSourceResolvable resolvable, Locale locale) thro
9292
}
9393
}
9494

95+
96+
@Override
97+
public String toString() {
98+
return this.parentMessageSource != null ? this.parentMessageSource.toString() : "Empty MessageSource";
99+
}
100+
95101
}

spring-web/src/main/java/org/springframework/web/context/ContextLoader.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,7 @@ else if (ccl != null) {
303303

304304
if (logger.isInfoEnabled()) {
305305
long elapsedTime = System.currentTimeMillis() - startTime;
306-
logger.info("Root WebApplicationContext: initialization completed in " + elapsedTime + " ms");
306+
logger.info("Root WebApplicationContext initialized in " + elapsedTime + " ms");
307307
}
308308

309309
return this.context;

spring-web/src/main/java/org/springframework/web/context/support/AnnotationConfigWebApplicationContext.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ protected void loadBeanDefinitions(DefaultListableBeanFactory beanFactory) {
230230
try {
231231
Class<?> clazz = ClassUtils.forName(configLocation, getClassLoader());
232232
if (logger.isTraceEnabled()) {
233-
logger.trace("Successfully resolved class for [" + configLocation + "]");
233+
logger.trace("Registering [" + configLocation + "]");
234234
}
235235
reader.register(clazz);
236236
}

spring-webmvc/src/main/java/org/springframework/web/servlet/FrameworkServlet.java

+3-20
Original file line numberDiff line numberDiff line change
@@ -516,9 +516,9 @@ public void setApplicationContext(ApplicationContext applicationContext) {
516516
*/
517517
@Override
518518
protected final void initServletBean() throws ServletException {
519-
getServletContext().log("Initializing Spring FrameworkServlet '" + getServletName() + "'");
519+
getServletContext().log("Initializing Spring " + getClass().getSimpleName() + " '" + getServletName() + "'");
520520
if (logger.isInfoEnabled()) {
521-
logger.info("FrameworkServlet '" + getServletName() + "': initialization started");
521+
logger.info("Initializing Servlet '" + getServletName() + "'");
522522
}
523523
long startTime = System.currentTimeMillis();
524524

@@ -540,9 +540,7 @@ protected final void initServletBean() throws ServletException {
540540
}
541541

542542
if (logger.isInfoEnabled()) {
543-
long elapsedTime = System.currentTimeMillis() - startTime;
544-
logger.info("FrameworkServlet '" + getServletName() + "': initialization completed in " +
545-
elapsedTime + " ms");
543+
logger.info("Completed initialization in " + (System.currentTimeMillis() - startTime) + " ms");
546544
}
547545
}
548546

@@ -600,10 +598,6 @@ protected WebApplicationContext initWebApplicationContext() {
600598
// Publish the context as a servlet context attribute.
601599
String attrName = getServletContextAttributeName();
602600
getServletContext().setAttribute(attrName, wac);
603-
if (logger.isTraceEnabled()) {
604-
logger.trace("Published WebApplicationContext of servlet '" + getServletName() +
605-
"' as ServletContext attribute [" + attrName + "]");
606-
}
607601
}
608602

609603
return wac;
@@ -650,11 +644,6 @@ protected WebApplicationContext findWebApplicationContext() {
650644
*/
651645
protected WebApplicationContext createWebApplicationContext(@Nullable ApplicationContext parent) {
652646
Class<?> contextClass = getContextClass();
653-
if (logger.isTraceEnabled()) {
654-
logger.trace("Servlet '" + getServletName() +
655-
"' will create custom WebApplicationContext context of class '" +
656-
contextClass.getName() + "'" + ", parent context [" + parent + "]");
657-
}
658647
if (!ConfigurableWebApplicationContext.class.isAssignableFrom(contextClass)) {
659648
throw new ApplicationContextException(
660649
"Fatal initialization error in servlet with name '" + getServletName() +
@@ -1071,19 +1060,13 @@ private void initContextHolders(HttpServletRequest request,
10711060
if (requestAttributes != null) {
10721061
RequestContextHolder.setRequestAttributes(requestAttributes, this.threadContextInheritable);
10731062
}
1074-
if (logger.isTraceEnabled()) {
1075-
logger.trace("Bound request context to thread: " + request);
1076-
}
10771063
}
10781064

10791065
private void resetContextHolders(HttpServletRequest request,
10801066
@Nullable LocaleContext prevLocaleContext, @Nullable RequestAttributes previousAttributes) {
10811067

10821068
LocaleContextHolder.setLocaleContext(prevLocaleContext, this.threadContextInheritable);
10831069
RequestContextHolder.setRequestAttributes(previousAttributes, this.threadContextInheritable);
1084-
if (logger.isTraceEnabled()) {
1085-
logger.trace("Cleared thread-bound request context: " + request);
1086-
}
10871070
}
10881071

10891072
private void logResult(HttpServletRequest request, HttpServletResponse response,

spring-webmvc/src/main/java/org/springframework/web/servlet/HttpServletBean.java

-7
Original file line numberDiff line numberDiff line change
@@ -146,9 +146,6 @@ protected ConfigurableEnvironment createEnvironment() {
146146
*/
147147
@Override
148148
public final void init() throws ServletException {
149-
if (logger.isTraceEnabled()) {
150-
logger.trace("Initializing servlet '" + getServletName() + "'");
151-
}
152149

153150
// Set bean properties from init parameters.
154151
PropertyValues pvs = new ServletConfigPropertyValues(getServletConfig(), this.requiredProperties);
@@ -170,10 +167,6 @@ public final void init() throws ServletException {
170167

171168
// Let subclasses do whatever initialization they like.
172169
initServletBean();
173-
174-
if (logger.isTraceEnabled()) {
175-
logger.trace("Servlet '" + getServletName() + "' configured successfully");
176-
}
177170
}
178171

179172
/**

0 commit comments

Comments
 (0)