Improve Servlet and Filter registration logging

Prior to this commit, the information logged at DEBUG level for the web
infrastructure was not consistent with the experience in Spring
Framework.

This commit moves some logging information about
`ServletContextInitializer` registrations from DEBUG to TRACE level and
gathers the Servlets and Filters registration logs into one-liners at
the DEBUG level.

Closes gh-15166
pull/15225/head
Brian Clozel 6 years ago
parent 3fac0f0360
commit d52b599eee

@ -131,6 +131,8 @@ public class LoggingApplicationListener implements GenericApplicationListener {
loggers.add("web", "org.springframework.http"); loggers.add("web", "org.springframework.http");
loggers.add("web", "org.springframework.web"); loggers.add("web", "org.springframework.web");
loggers.add("web", "org.springframework.boot.actuate.endpoint.web"); loggers.add("web", "org.springframework.boot.actuate.endpoint.web");
loggers.add("web",
"org.springframework.boot.web.servlet.ServletContextInitializerBeans");
loggers.add("sql", "org.springframework.jdbc.core"); loggers.add("sql", "org.springframework.jdbc.core");
loggers.add("sql", "org.hibernate.SQL"); loggers.add("sql", "org.hibernate.SQL");
DEFAULT_GROUP_LOGGERS = Collections.unmodifiableMap(loggers); DEFAULT_GROUP_LOGGERS = Collections.unmodifiableMap(loggers);

@ -41,6 +41,7 @@ import org.springframework.util.StringUtils;
* *
* @param <T> the type of {@link Filter} to register * @param <T> the type of {@link Filter} to register
* @author Phillip Webb * @author Phillip Webb
* @author Brian Clozel
* @since 2.0.1 * @since 2.0.1
*/ */
public abstract class AbstractFilterRegistrationBean<T extends Filter> public abstract class AbstractFilterRegistrationBean<T extends Filter>
@ -245,27 +246,38 @@ public abstract class AbstractFilterRegistrationBean<T extends Filter>
} }
servletNames.addAll(this.servletNames); servletNames.addAll(this.servletNames);
if (servletNames.isEmpty() && this.urlPatterns.isEmpty()) { if (servletNames.isEmpty() && this.urlPatterns.isEmpty()) {
this.logger.info("Mapping filter: '" + registration.getName() + "' to: "
+ Arrays.asList(DEFAULT_URL_MAPPINGS));
registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter, registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter,
DEFAULT_URL_MAPPINGS); DEFAULT_URL_MAPPINGS);
} }
else { else {
if (!servletNames.isEmpty()) { if (!servletNames.isEmpty()) {
this.logger.info("Mapping filter: '" + registration.getName()
+ "' to servlets: " + servletNames);
registration.addMappingForServletNames(dispatcherTypes, this.matchAfter, registration.addMappingForServletNames(dispatcherTypes, this.matchAfter,
StringUtils.toStringArray(servletNames)); StringUtils.toStringArray(servletNames));
} }
if (!this.urlPatterns.isEmpty()) { if (!this.urlPatterns.isEmpty()) {
this.logger.info("Mapping filter: '" + registration.getName()
+ "' to urls: " + this.urlPatterns);
registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter, registration.addMappingForUrlPatterns(dispatcherTypes, this.matchAfter,
StringUtils.toStringArray(this.urlPatterns)); StringUtils.toStringArray(this.urlPatterns));
} }
} }
} }
@Override
public String toString() {
StringBuilder builder = new StringBuilder(getOrDeduceName(this));
if (this.servletNames.isEmpty() && this.urlPatterns.isEmpty()) {
builder.append(" urls=").append(Arrays.toString(DEFAULT_URL_MAPPINGS));
}
else {
if (!this.servletNames.isEmpty()) {
builder.append(" servlets=").append(this.servletNames);
}
if (!this.urlPatterns.isEmpty()) {
builder.append(" urls=").append(this.urlPatterns);
}
}
return builder.toString();
}
/** /**
* Return the {@link Filter} to be registered. * Return the {@link Filter} to be registered.
* @return the filter * @return the filter

@ -57,6 +57,7 @@ import org.springframework.util.MultiValueMap;
* *
* @author Dave Syer * @author Dave Syer
* @author Phillip Webb * @author Phillip Webb
* @author Brian Clozel
* @since 1.4.0 * @since 1.4.0
*/ */
public class ServletContextInitializerBeans public class ServletContextInitializerBeans
@ -93,6 +94,7 @@ public class ServletContextInitializerBeans
.sorted(AnnotationAwareOrderComparator.INSTANCE)) .sorted(AnnotationAwareOrderComparator.INSTANCE))
.collect(Collectors.toList()); .collect(Collectors.toList());
this.sortedList = Collections.unmodifiableList(sortedInitializers); this.sortedList = Collections.unmodifiableList(sortedInitializers);
logBeansInformation(this.initializers);
} }
private void addServletContextInitializerBeans(ListableBeanFactory beanFactory) { private void addServletContextInitializerBeans(ListableBeanFactory beanFactory) {
@ -143,10 +145,10 @@ public class ServletContextInitializerBeans
// Mark the underlying source as seen in case it wraps an existing bean // Mark the underlying source as seen in case it wraps an existing bean
this.seen.add(source); this.seen.add(source);
} }
if (ServletContextInitializerBeans.logger.isDebugEnabled()) { if (ServletContextInitializerBeans.logger.isTraceEnabled()) {
String resourceDescription = getResourceDescription(beanName, beanFactory); String resourceDescription = getResourceDescription(beanName, beanFactory);
int order = getOrder(initializer); int order = getOrder(initializer);
ServletContextInitializerBeans.logger.debug("Added existing " ServletContextInitializerBeans.logger.trace("Added existing "
+ type.getSimpleName() + " initializer bean '" + beanName + type.getSimpleName() + " initializer bean '" + beanName
+ "'; order=" + order + ", resource=" + resourceDescription); + "'; order=" + order + ", resource=" + resourceDescription);
} }
@ -200,8 +202,8 @@ public class ServletContextInitializerBeans
bean.getValue(), beans.size()); bean.getValue(), beans.size());
registration.setOrder(order); registration.setOrder(order);
this.initializers.add(type, registration); this.initializers.add(type, registration);
if (ServletContextInitializerBeans.logger.isDebugEnabled()) { if (ServletContextInitializerBeans.logger.isTraceEnabled()) {
ServletContextInitializerBeans.logger.debug( ServletContextInitializerBeans.logger.trace(
"Created " + type.getSimpleName() + " initializer for bean '" "Created " + type.getSimpleName() + " initializer for bean '"
+ beanName + "'; order=" + order + ", resource=" + beanName + "'; order=" + order + ", resource="
+ getResourceDescription(beanName, beanFactory)); + getResourceDescription(beanName, beanFactory));
@ -245,6 +247,36 @@ public class ServletContextInitializerBeans
return beans; return beans;
} }
private void logBeansInformation(
MultiValueMap<Class<?>, ServletContextInitializer> initializers) {
if (ServletContextInitializerBeans.logger.isDebugEnabled()) {
List<ServletContextInitializer> filterRegistrations = new ArrayList<>();
filterRegistrations.addAll(initializers
.getOrDefault(FilterRegistrationBean.class, Collections.emptyList()));
filterRegistrations.addAll(
initializers.getOrDefault(Filter.class, Collections.emptyList()));
String filtersInfo = filterRegistrations.stream()
.map(FilterRegistrationBean.class::cast)
.map(FilterRegistrationBean::toString)
.collect(Collectors.joining(", "));
ServletContextInitializerBeans.logger
.debug("Mapping filters: " + filtersInfo);
List<ServletContextInitializer> servletRegistrations = new ArrayList<>();
servletRegistrations.addAll(initializers.getOrDefault(
ServletRegistrationBean.class, Collections.emptyList()));
servletRegistrations.addAll(
initializers.getOrDefault(Servlet.class, Collections.emptyList()));
String servletsInfo = servletRegistrations.stream()
.map(ServletRegistrationBean.class::cast)
.map((servlet) -> servlet.getServletName() + " "
+ servlet.getUrlMappings())
.collect(Collectors.joining(", "));
ServletContextInitializerBeans.logger
.debug("Mapping servlets: " + servletsInfo);
}
}
@Override @Override
public Iterator<ServletContextInitializer> iterator() { public Iterator<ServletContextInitializer> iterator() {
return this.sortedList.iterator(); return this.sortedList.iterator();

@ -183,7 +183,6 @@ public class ServletRegistrationBean<T extends Servlet>
protected ServletRegistration.Dynamic addRegistration(String description, protected ServletRegistration.Dynamic addRegistration(String description,
ServletContext servletContext) { ServletContext servletContext) {
String name = getServletName(); String name = getServletName();
logger.info("Servlet " + name + " mapped to " + this.urlMappings);
return servletContext.addServlet(name, this.servlet); return servletContext.addServlet(name, this.servlet);
} }

@ -570,6 +570,9 @@ public class LoggingApplicationListenerTests {
assertTraceEnabled("org.springframework.http", true); assertTraceEnabled("org.springframework.http", true);
assertTraceEnabled("org.springframework.web", true); assertTraceEnabled("org.springframework.web", true);
assertTraceEnabled("org.springframework.boot.actuate.endpoint.web", true); assertTraceEnabled("org.springframework.boot.actuate.endpoint.web", true);
assertTraceEnabled(
"org.springframework.boot.web.servlet.ServletContextInitializerBeans",
true);
} }
@Test @Test

Loading…
Cancel
Save