Skip to content

Commit c89e6c6

Browse files
committed
Improve logging of request mapping information
Issue: SPR-17450
1 parent 539cfc2 commit c89e6c6

File tree

4 files changed

+68
-16
lines changed

4 files changed

+68
-16
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/AbstractHandlerMethodMapping.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.Collection;
2223
import java.util.Collections;
2324
import java.util.Comparator;
@@ -28,6 +29,8 @@
2829
import java.util.Set;
2930
import java.util.concurrent.ConcurrentHashMap;
3031
import java.util.concurrent.locks.ReentrantReadWriteLock;
32+
import java.util.function.Function;
33+
import java.util.stream.Collectors;
3134

3235
import reactor.core.publisher.Mono;
3336

@@ -202,7 +205,7 @@ protected void detectHandlerMethods(final Object handler) {
202205
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
203206
(MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
204207
if (logger.isTraceEnabled()) {
205-
logger.trace("Mapped " + methods.size() + " handler method(s) for " + userType + ": " + methods);
208+
logger.trace(formatMappings(userType, methods));
206209
}
207210
methods.forEach((key, mapping) -> {
208211
Method invocableMethod = AopUtils.selectInvocableMethod(key, userType);
@@ -211,6 +214,24 @@ protected void detectHandlerMethods(final Object handler) {
211214
}
212215
}
213216

217+
private String formatMappings(Class<?> userType, Map<Method, T> methods) {
218+
219+
String formattedType = Arrays.stream(userType.getPackage().getName().split("\\."))
220+
.map(p -> p.substring(0, 1))
221+
.collect(Collectors.joining(".", "", ".")) + userType.getSimpleName();
222+
223+
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
224+
.map(Class::getSimpleName)
225+
.collect(Collectors.joining(",", "(", ")"));
226+
227+
return methods.entrySet().stream()
228+
.map(e -> {
229+
Method method = e.getKey();
230+
return e.getValue() + ": " + method.getName() + methodFormatter.apply(method);
231+
})
232+
.collect(Collectors.joining("\n\t", "\n\t" + formattedType + ":" + "\n\t", ""));
233+
}
234+
214235
/**
215236
* Register a handler method and its unique mapping. Invoked at startup for
216237
* each detected handler method.

spring-webflux/src/main/java/org/springframework/web/reactive/result/method/RequestMappingInfo.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.util.Arrays;
2020
import java.util.List;
21+
import java.util.Set;
2122
import java.util.stream.Collectors;
2223

2324
import org.springframework.lang.Nullable;
@@ -307,24 +308,28 @@ public int hashCode() {
307308
@Override
308309
public String toString() {
309310
StringBuilder builder = new StringBuilder("{");
310-
builder.append(this.patternsCondition);
311311
if (!this.methodsCondition.isEmpty()) {
312-
builder.append(",methods=").append(this.methodsCondition);
312+
Set<RequestMethod> httpMethods = this.methodsCondition.getMethods();
313+
builder.append(httpMethods.size() == 1 ? httpMethods.iterator().next() : httpMethods);
314+
}
315+
if (!this.patternsCondition.isEmpty()) {
316+
Set<PathPattern> patterns = this.patternsCondition.getPatterns();
317+
builder.append(" ").append(patterns.size() == 1 ? patterns.iterator().next() : patterns);
313318
}
314319
if (!this.paramsCondition.isEmpty()) {
315-
builder.append(",params=").append(this.paramsCondition);
320+
builder.append(", params ").append(this.paramsCondition);
316321
}
317322
if (!this.headersCondition.isEmpty()) {
318-
builder.append(",headers=").append(this.headersCondition);
323+
builder.append(", headers ").append(this.headersCondition);
319324
}
320325
if (!this.consumesCondition.isEmpty()) {
321-
builder.append(",consumes=").append(this.consumesCondition);
326+
builder.append(", consumes ").append(this.consumesCondition);
322327
}
323328
if (!this.producesCondition.isEmpty()) {
324-
builder.append(",produces=").append(this.producesCondition);
329+
builder.append(", produces ").append(this.producesCondition);
325330
}
326331
if (!this.customConditionHolder.isEmpty()) {
327-
builder.append(",custom=").append(this.customConditionHolder);
332+
builder.append(", and ").append(this.customConditionHolder);
328333
}
329334
builder.append('}');
330335
return builder.toString();

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
import java.lang.reflect.Method;
2020
import java.util.ArrayList;
21+
import java.util.Arrays;
2122
import java.util.Collection;
2223
import java.util.Collections;
2324
import java.util.Comparator;
@@ -28,6 +29,8 @@
2829
import java.util.Set;
2930
import java.util.concurrent.ConcurrentHashMap;
3031
import java.util.concurrent.locks.ReentrantReadWriteLock;
32+
import java.util.function.Function;
33+
import java.util.stream.Collectors;
3134
import javax.servlet.ServletException;
3235
import javax.servlet.http.HttpServletRequest;
3336

@@ -272,7 +275,7 @@ protected void detectHandlerMethods(Object handler) {
272275
}
273276
});
274277
if (logger.isTraceEnabled()) {
275-
logger.trace("Mapped " + methods.size() + " handler method(s) for " + userType + ": " + methods);
278+
logger.trace(formatMappings(userType, methods));
276279
}
277280
methods.forEach((method, mapping) -> {
278281
Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
@@ -281,6 +284,24 @@ protected void detectHandlerMethods(Object handler) {
281284
}
282285
}
283286

287+
private String formatMappings(Class<?> userType, Map<Method, T> methods) {
288+
289+
String formattedType = Arrays.stream(userType.getPackage().getName().split("\\."))
290+
.map(p -> p.substring(0, 1))
291+
.collect(Collectors.joining(".", "", ".")) + userType.getSimpleName();
292+
293+
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
294+
.map(Class::getSimpleName)
295+
.collect(Collectors.joining(",", "(", ")"));
296+
297+
return methods.entrySet().stream()
298+
.map(e -> {
299+
Method method = e.getKey();
300+
return e.getValue() + ": " + method.getName() + methodFormatter.apply(method);
301+
})
302+
.collect(Collectors.joining("\n\t", "\n\t" + formattedType + ":" + "\n\t", ""));
303+
}
304+
284305
/**
285306
* Register a handler method and its unique mapping. Invoked at startup for
286307
* each detected handler method.

spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/RequestMappingInfo.java

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package org.springframework.web.servlet.mvc.method;
1818

1919
import java.util.List;
20+
import java.util.Set;
2021
import javax.servlet.http.HttpServletRequest;
2122

2223
import org.springframework.http.HttpMethod;
@@ -316,24 +317,28 @@ public int hashCode() {
316317
@Override
317318
public String toString() {
318319
StringBuilder builder = new StringBuilder("{");
319-
builder.append(this.patternsCondition);
320320
if (!this.methodsCondition.isEmpty()) {
321-
builder.append(",methods=").append(this.methodsCondition);
321+
Set<RequestMethod> httpMethods = this.methodsCondition.getMethods();
322+
builder.append(httpMethods.size() == 1 ? httpMethods.iterator().next() : httpMethods);
323+
}
324+
if (!this.patternsCondition.isEmpty()) {
325+
Set<String> patterns = this.patternsCondition.getPatterns();
326+
builder.append(" ").append(patterns.size() == 1 ? patterns.iterator().next() : patterns);
322327
}
323328
if (!this.paramsCondition.isEmpty()) {
324-
builder.append(",params=").append(this.paramsCondition);
329+
builder.append(", params ").append(this.paramsCondition);
325330
}
326331
if (!this.headersCondition.isEmpty()) {
327-
builder.append(",headers=").append(this.headersCondition);
332+
builder.append(", headers ").append(this.headersCondition);
328333
}
329334
if (!this.consumesCondition.isEmpty()) {
330-
builder.append(",consumes=").append(this.consumesCondition);
335+
builder.append(", consumes ").append(this.consumesCondition);
331336
}
332337
if (!this.producesCondition.isEmpty()) {
333-
builder.append(",produces=").append(this.producesCondition);
338+
builder.append(", produces ").append(this.producesCondition);
334339
}
335340
if (!this.customConditionHolder.isEmpty()) {
336-
builder.append(",custom=").append(this.customConditionHolder);
341+
builder.append(", and ").append(this.customConditionHolder);
337342
}
338343
builder.append('}');
339344
return builder.toString();

0 commit comments

Comments
 (0)