Skip to content

Commit 19c024f

Browse files
committed
Improved logging for @MessageMapping methods
Closes gh-20564
1 parent b88aad6 commit 19c024f

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/AbstractMethodMessageHandler.java

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2019 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -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;
@@ -26,6 +27,8 @@
2627
import java.util.Map;
2728
import java.util.Set;
2829
import java.util.concurrent.ConcurrentHashMap;
30+
import java.util.function.Function;
31+
import java.util.stream.Collectors;
2932

3033
import org.apache.commons.logging.Log;
3134
import org.apache.commons.logging.LogFactory;
@@ -309,12 +312,27 @@ protected final void detectHandlerMethods(final Object handler) {
309312
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
310313
(MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
311314
if (logger.isDebugEnabled()) {
312-
logger.debug(methods.size() + " message handler methods found on " + userType + ": " + methods);
315+
logger.debug(formatMappings(userType, methods));
313316
}
314317
methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
315318
}
316319
}
317320

321+
private String formatMappings(Class<?> userType, Map<Method, T> methods) {
322+
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
323+
.map(p -> p.substring(0, 1))
324+
.collect(Collectors.joining(".", "", "." + userType.getSimpleName()));
325+
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
326+
.map(Class::getSimpleName)
327+
.collect(Collectors.joining(",", "(", ")"));
328+
return methods.entrySet().stream()
329+
.map(e -> {
330+
Method method = e.getKey();
331+
return e.getValue() + ": " + method.getName() + methodFormatter.apply(method);
332+
})
333+
.collect(Collectors.joining("\n\t", "\n\t" + formattedType + ":" + "\n\t", ""));
334+
}
335+
318336
/**
319337
* Provide the mapping for a handler method.
320338
* @param method the method to provide a mapping for
@@ -344,9 +362,6 @@ protected void registerHandlerMethod(Object handler, Method method, T mapping) {
344362
}
345363

346364
this.handlerMethods.put(mapping, newHandlerMethod);
347-
if (logger.isTraceEnabled()) {
348-
logger.trace("Mapped \"" + mapping + "\" onto " + newHandlerMethod);
349-
}
350365

351366
for (String pattern : getDirectLookupDestinations(mapping)) {
352367
this.destinationLookup.add(pattern, mapping);

spring-messaging/src/main/java/org/springframework/messaging/handler/invocation/reactive/AbstractMethodMessageHandler.java

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,17 @@
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;
2425
import java.util.LinkedHashMap;
2526
import java.util.List;
2627
import java.util.Map;
2728
import java.util.Set;
29+
import java.util.function.Function;
2830
import java.util.function.Predicate;
31+
import java.util.stream.Collectors;
2932

3033
import org.apache.commons.logging.Log;
3134
import org.apache.commons.logging.LogFactory;
@@ -284,12 +287,27 @@ protected final void detectHandlerMethods(Object handler) {
284287
Map<Method, T> methods = MethodIntrospector.selectMethods(userType,
285288
(MethodIntrospector.MetadataLookup<T>) method -> getMappingForMethod(method, userType));
286289
if (logger.isDebugEnabled()) {
287-
logger.debug(methods.size() + " message handler methods found on " + userType + ": " + methods);
290+
logger.debug(formatMappings(userType, methods));
288291
}
289292
methods.forEach((key, value) -> registerHandlerMethod(handler, key, value));
290293
}
291294
}
292295

296+
private String formatMappings(Class<?> userType, Map<Method, T> methods) {
297+
String formattedType = Arrays.stream(ClassUtils.getPackageName(userType).split("\\."))
298+
.map(p -> p.substring(0, 1))
299+
.collect(Collectors.joining(".", "", "." + userType.getSimpleName()));
300+
Function<Method, String> methodFormatter = method -> Arrays.stream(method.getParameterTypes())
301+
.map(Class::getSimpleName)
302+
.collect(Collectors.joining(",", "(", ")"));
303+
return methods.entrySet().stream()
304+
.map(e -> {
305+
Method method = e.getKey();
306+
return e.getValue() + ": " + method.getName() + methodFormatter.apply(method);
307+
})
308+
.collect(Collectors.joining("\n\t", "\n\t" + formattedType + ":" + "\n\t", ""));
309+
}
310+
293311
/**
294312
* Obtain the mapping for the given method, if any.
295313
* @param method the method to check
@@ -322,9 +340,6 @@ protected final void registerHandlerMethod(Object handler, Method method, T mapp
322340
}
323341

324342
this.handlerMethods.put(mapping, newHandlerMethod);
325-
if (logger.isTraceEnabled()) {
326-
logger.trace("Mapped \"" + mapping + "\" onto " + newHandlerMethod);
327-
}
328343

329344
for (String pattern : getDirectLookupMappings(mapping)) {
330345
this.destinationLookup.add(pattern, mapping);

0 commit comments

Comments
 (0)