Dedicated, "_"-prefixed log category for request mappings

Closes gh-26539
This commit is contained in:
Rossen Stoyanchev
2021-02-22 20:37:56 +00:00
parent cb3af52df2
commit 3ec0452fed
13 changed files with 122 additions and 39 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2020 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -153,14 +153,31 @@ public class RouterFunctionMapping extends AbstractHandlerMapping implements Ini
(this.detectHandlerFunctionsInAncestorContexts ?
BeanFactoryUtils.beansOfTypeIncludingAncestors(applicationContext, RouterFunction.class) :
applicationContext.getBeansOfType(RouterFunction.class));
List<RouterFunction> routerFunctions = new ArrayList<>(beans.values());
if (!CollectionUtils.isEmpty(routerFunctions) && logger.isInfoEnabled()) {
routerFunctions.forEach(routerFunction -> logger.info("Mapped " + routerFunction));
this.routerFunction = routerFunctions.stream().reduce(RouterFunction::andOther).orElse(null);
logRouterFunctions(routerFunctions);
}
@SuppressWarnings("rawtypes")
private void logRouterFunctions(List<RouterFunction> routerFunctions) {
if (mappingsLogger.isDebugEnabled()) {
routerFunctions.forEach(function -> mappingsLogger.debug("Mapped " + function));
}
else if (logger.isDebugEnabled()) {
int total = routerFunctions.size();
String message = total + " RouterFunction(s) in " + formatMappingName();
if (logger.isTraceEnabled()) {
if (total > 0) {
routerFunctions.forEach(function -> logger.trace("Mapped " + function));
}
else {
logger.trace(message);
}
}
else if (total > 0) {
logger.debug(message);
}
}
this.routerFunction = routerFunctions.stream()
.reduce(RouterFunction::andOther)
.orElse(null);
}
/**

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2012 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -82,7 +82,10 @@ public abstract class AbstractDetectingUrlHandlerMapping extends AbstractUrlHand
}
}
if ((logger.isDebugEnabled() && !getHandlerMap().isEmpty()) || logger.isTraceEnabled()) {
if (mappingsLogger.isDebugEnabled()) {
mappingsLogger.debug(formatMappingName() + " " + getHandlerMap());
}
else if ((logger.isDebugEnabled() && !getHandlerMap().isEmpty()) || logger.isTraceEnabled()) {
logger.debug("Detected " + getHandlerMap().size() + " mappings in " + formatMappingName());
}
}

View File

@@ -26,10 +26,13 @@ import javax.servlet.DispatcherType;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.logging.Log;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.BeanFactoryUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.core.Ordered;
import org.springframework.core.log.LogDelegateFactory;
import org.springframework.http.server.RequestPath;
import org.springframework.lang.Nullable;
import org.springframework.util.AntPathMatcher;
@@ -75,6 +78,11 @@ import org.springframework.web.util.pattern.PathPatternParser;
public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
implements HandlerMapping, Ordered, BeanNameAware {
/** Dedicated "hidden" logger for request mappings. */
protected final Log mappingsLogger =
LogDelegateFactory.getHiddenLog(HandlerMapping.class.getName() + ".Mappings");
@Nullable
private Object defaultHandler;
@@ -358,7 +366,7 @@ public abstract class AbstractHandlerMapping extends WebApplicationObjectSupport
}
protected String formatMappingName() {
return this.beanName != null ? "'" + this.beanName + "'" : "<unknown>";
return this.beanName != null ? "'" + this.beanName + "'" : getClass().getName();
}

View File

@@ -290,6 +290,9 @@ public abstract class AbstractHandlerMethodMapping<T> extends AbstractHandlerMap
if (logger.isTraceEnabled()) {
logger.trace(formatMappings(userType, methods));
}
else if (mappingsLogger.isDebugEnabled()) {
mappingsLogger.debug(formatMappings(userType, methods));
}
methods.forEach((method, mapping) -> {
Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
registerHandlerMethod(handler, invocableMethod, mapping);

View File

@@ -206,7 +206,7 @@ public abstract class AbstractUrlHandlerMapping extends AbstractHandlerMapping i
if (matches.size() > 1) {
matches.sort(PathPattern.SPECIFICITY_COMPARATOR);
if (logger.isTraceEnabled()) {
logger.debug("Matching patterns " + matches);
logger.trace("Matching patterns " + matches);
}
}
PathPattern pattern = matches.get(0);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2002-2019 the original author or authors.
* Copyright 2002-2021 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -161,17 +161,31 @@ public class SimpleUrlHandlerMapping extends AbstractUrlHandlerMapping {
}
registerHandler(url, handler);
});
if (logger.isDebugEnabled()) {
List<String> patterns = new ArrayList<>();
if (getRootHandler() != null) {
patterns.add("/");
}
if (getDefaultHandler() != null) {
patterns.add("/**");
}
patterns.addAll(getHandlerMap().keySet());
logger.debug("Patterns " + patterns + " in " + formatMappingName());
logMappings();
}
}
private void logMappings() {
if (mappingsLogger.isDebugEnabled()) {
Map<String, Object> map = new LinkedHashMap<>(getHandlerMap());
if (getRootHandler() != null) {
map.put("/", getRootHandler());
}
if (getDefaultHandler() != null) {
map.put("/**", getDefaultHandler());
}
mappingsLogger.debug(formatMappingName() + " " + map);
}
else if (logger.isDebugEnabled()) {
List<String> patterns = new ArrayList<>();
if (getRootHandler() != null) {
patterns.add("/");
}
if (getDefaultHandler() != null) {
patterns.add("/**");
}
patterns.addAll(getHandlerMap().keySet());
logger.debug("Patterns " + patterns + " in " + formatMappingName());
}
}

View File

@@ -800,6 +800,10 @@ public class ResourceHttpRequestHandler extends WebContentGenerator
@Override
public String toString() {
return "ResourceHttpRequestHandler " + getLocations();
return "ResourceHttpRequestHandler " +
getLocations().toString()
.replaceAll("class path resource", "Classpath")
.replaceAll("ServletContext resource", "ServletContext");
}
}