Skip to content

Commit 833aee9

Browse files
committed
Add SimpLogging and use o.s.messaging.simp classes
Issue: SPR-17012
1 parent 4d6f2df commit 833aee9

19 files changed

+239
-32
lines changed

spring-messaging/src/main/java/org/springframework/messaging/handler/HandlerMethod.java

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,11 @@
5050
*/
5151
public class HandlerMethod {
5252

53-
/** Logger that is available to subclasses. */
54-
protected final Log logger = LogFactory.getLog(getClass());
53+
/** Public for wrapping with fallback logger. */
54+
public static final Log defaultLogger = LogFactory.getLog(HandlerMethod.class);
55+
56+
57+
protected Log logger = defaultLogger;
5558

5659
private final Object bean;
5760

@@ -161,6 +164,24 @@ private MethodParameter[] initMethodParameters() {
161164
return result;
162165
}
163166

167+
168+
/**
169+
* Set an alternative logger to use than the one based on the class name.
170+
* @param logger the logger to use
171+
* @since 5.1
172+
*/
173+
public void setLogger(Log logger) {
174+
this.logger = logger;
175+
}
176+
177+
/**
178+
* Return the currently configured Logger.
179+
* @since 5.1
180+
*/
181+
public Log getLogger() {
182+
return logger;
183+
}
184+
164185
/**
165186
* Return the bean for this handler method.
166187
*/

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,10 @@ public abstract class AbstractMethodMessageHandler<T>
8585

8686
protected final Log logger = LogFactory.getLog(getClass());
8787

88+
@Nullable
89+
private Log handlerMethodLogger;
90+
91+
8892
private final List<String> destinationPrefixes = new ArrayList<>();
8993

9094
private final List<HandlerMethodArgumentResolver> customArgumentResolvers = new ArrayList<>(4);
@@ -232,6 +236,12 @@ public void afterPropertiesSet() {
232236
if (this.returnValueHandlers.getReturnValueHandlers().isEmpty()) {
233237
this.returnValueHandlers.addHandlers(initReturnValueHandlers());
234238
}
239+
Log returnValueLogger = getReturnValueHandlerLogger();
240+
if (returnValueLogger != null) {
241+
this.returnValueHandlers.setLogger(returnValueLogger);
242+
}
243+
244+
this.handlerMethodLogger = getHandlerMethodLogger();
235245

236246
ApplicationContext context = getApplicationContext();
237247
if (context == null) {
@@ -367,6 +377,24 @@ protected HandlerMethod createHandlerMethod(Object handler, Method method) {
367377
*/
368378
protected abstract Set<String> getDirectLookupDestinations(T mapping);
369379

380+
/**
381+
* Return a logger to set on {@link HandlerMethodReturnValueHandlerComposite}.
382+
* @since 5.1
383+
*/
384+
@Nullable
385+
protected Log getReturnValueHandlerLogger() {
386+
return null;
387+
}
388+
389+
/**
390+
* Return a logger to set on {@link InvocableHandlerMethod}.
391+
* @since 5.1
392+
*/
393+
@Nullable
394+
protected Log getHandlerMethodLogger() {
395+
return null;
396+
}
397+
370398
/**
371399
* Subclasses can invoke this method to populate the MessagingAdviceBean cache
372400
* (e.g. to support "global" {@code @MessageExceptionHandler}).
@@ -514,6 +542,9 @@ protected void handleMatch(T mapping, HandlerMethod handlerMethod, String lookup
514542
}
515543
handlerMethod = handlerMethod.createWithResolvedBean();
516544
InvocableHandlerMethod invocable = new InvocableHandlerMethod(handlerMethod);
545+
if (this.handlerMethodLogger != null) {
546+
invocable.setLogger(this.handlerMethodLogger);
547+
}
517548
invocable.setMessageMethodArgumentResolvers(this.argumentResolvers);
518549
try {
519550
Object returnValue = invocable.invoke(message);

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

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,32 @@
3737
*/
3838
public class HandlerMethodReturnValueHandlerComposite implements AsyncHandlerMethodReturnValueHandler {
3939

40-
private static final Log logger = LogFactory.getLog(HandlerMethodReturnValueHandlerComposite.class);
40+
/** Public for wrapping with fallback logger. */
41+
public static final Log defaultLogger = LogFactory.getLog(HandlerMethodReturnValueHandlerComposite.class);
42+
43+
44+
private Log logger = defaultLogger;
4145

4246
private final List<HandlerMethodReturnValueHandler> returnValueHandlers = new ArrayList<>();
4347

4448

49+
/**
50+
* Set an alternative logger to use than the one based on the class name.
51+
* @param logger the logger to use
52+
* @since 5.1
53+
*/
54+
public void setLogger(Log logger) {
55+
this.logger = logger;
56+
}
57+
58+
/**
59+
* Return the currently configured Logger.
60+
* @since 5.1
61+
*/
62+
public Log getLogger() {
63+
return logger;
64+
}
65+
4566
/**
4667
* Return a read-only list with the configured handlers.
4768
*/

spring-messaging/src/main/java/org/springframework/messaging/simp/SimpAttributes.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
import java.util.Map;
2020

2121
import org.apache.commons.logging.Log;
22-
import org.apache.commons.logging.LogFactory;
2322

2423
import org.springframework.lang.Nullable;
2524
import org.springframework.messaging.Message;
@@ -46,7 +45,7 @@ public class SimpAttributes {
4645
public static final String DESTRUCTION_CALLBACK_NAME_PREFIX =
4746
SimpAttributes.class.getName() + ".DESTRUCTION_CALLBACK.";
4847

49-
private static final Log logger = LogFactory.getLog(SimpAttributes.class);
48+
private static final Log logger = SimpLogging.forLogName(SimpAttributes.class);
5049

5150

5251
private final String sessionId;
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
/*
2+
* Copyright 2002-2018 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.springframework.messaging.simp;
17+
18+
import org.apache.commons.logging.Log;
19+
import org.apache.commons.logging.LogFactory;
20+
21+
import org.springframework.util.log.LogUtils;
22+
23+
/**
24+
* Holds the shared logger named "org.springframework.web.SimpLogging" to use
25+
* for STOMP over WebSocket messaging when logging for
26+
* "org.springframework.messaging.simp" is off but logging for
27+
* "org.springframework.web" is on.
28+
*
29+
* <p>This makes it possible to enable all web related logging via
30+
* "org.springframework.web" including logging from lower-level packages such as
31+
* "org.springframework.messaging.simp".
32+
*
33+
* <p>To see logging from the primary classes where log messages originate from,
34+
* simply enable logging for "org.springframework.messaging".
35+
*
36+
* @author Rossen Stoyanchev
37+
* @since 5.1
38+
*/
39+
public abstract class SimpLogging {
40+
41+
private static final Log fallbackLogger =
42+
LogFactory.getLog("org.springframework.web." + SimpLogging.class.getSimpleName());
43+
44+
45+
/**
46+
* Create a primary logger for the given class and wrap it with a composite
47+
* that delegates to it or to the fallback logger named
48+
* "org.springframework.web.SimpLogging", if the primary is not enabled.
49+
* @param primaryLoggerClass the class for the name of the primary logger
50+
* @return the resulting composite logger
51+
*/
52+
public static Log forLogName(Class<?> primaryLoggerClass) {
53+
Log primaryLogger = LogFactory.getLog(primaryLoggerClass);
54+
return forLog(primaryLogger);
55+
}
56+
57+
/**
58+
* Wrap the given primary logger with a composite logger that delegates to
59+
* either the primary or to the shared fallback logger
60+
* "org.springframework.web.HttpLogging", if the primary is not enabled.
61+
* @param primaryLogger the primary logger to use
62+
* @return the resulting composite logger
63+
*/
64+
public static Log forLog(Log primaryLogger) {
65+
return LogUtils.getCompositeLog(primaryLogger, fallbackLogger);
66+
}
67+
68+
}

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandler.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
import java.util.Map;
2626
import java.util.Set;
2727

28+
import org.apache.commons.logging.Log;
29+
2830
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
2931
import org.springframework.context.ApplicationContext;
3032
import org.springframework.context.ConfigurableApplicationContext;
@@ -56,8 +58,10 @@
5658
import org.springframework.messaging.handler.invocation.CompletableFutureReturnValueHandler;
5759
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
5860
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
61+
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandlerComposite;
5962
import org.springframework.messaging.handler.invocation.ListenableFutureReturnValueHandler;
6063
import org.springframework.messaging.simp.SimpAttributesContextHolder;
64+
import org.springframework.messaging.simp.SimpLogging;
6165
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
6266
import org.springframework.messaging.simp.SimpMessageMappingInfo;
6367
import org.springframework.messaging.simp.SimpMessageSendingOperations;
@@ -357,6 +361,16 @@ protected List<? extends HandlerMethodReturnValueHandler> initReturnValueHandler
357361
return handlers;
358362
}
359363

364+
@Override
365+
protected Log getReturnValueHandlerLogger() {
366+
return SimpLogging.forLog(HandlerMethodReturnValueHandlerComposite.defaultLogger);
367+
}
368+
369+
@Override
370+
protected Log getHandlerMethodLogger() {
371+
return SimpLogging.forLog(HandlerMethod.defaultLogger);
372+
}
373+
360374

361375
@Override
362376
protected boolean isHandler(Class<?> beanType) {

spring-messaging/src/main/java/org/springframework/messaging/simp/annotation/support/SubscriptionMethodReturnValueHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
package org.springframework.messaging.simp.annotation.support;
1818

1919
import org.apache.commons.logging.Log;
20-
import org.apache.commons.logging.LogFactory;
2120

2221
import org.springframework.core.MethodParameter;
2322
import org.springframework.lang.Nullable;
@@ -26,6 +25,7 @@
2625
import org.springframework.messaging.core.MessageSendingOperations;
2726
import org.springframework.messaging.handler.annotation.SendTo;
2827
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
28+
import org.springframework.messaging.simp.SimpLogging;
2929
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
3030
import org.springframework.messaging.simp.SimpMessageType;
3131
import org.springframework.messaging.simp.SimpMessagingTemplate;
@@ -58,7 +58,7 @@
5858
*/
5959
public class SubscriptionMethodReturnValueHandler implements HandlerMethodReturnValueHandler {
6060

61-
private static final Log logger = LogFactory.getLog(SubscriptionMethodReturnValueHandler.class);
61+
private static final Log logger = SimpLogging.forLogName(SubscriptionMethodReturnValueHandler.class);
6262

6363

6464
private final MessageSendingOperations<String> messagingTemplate;

spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractBrokerMessageHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
import java.util.concurrent.atomic.AtomicBoolean;
2222

2323
import org.apache.commons.logging.Log;
24-
import org.apache.commons.logging.LogFactory;
2524

2625
import org.springframework.context.ApplicationEventPublisher;
2726
import org.springframework.context.ApplicationEventPublisherAware;
@@ -31,6 +30,7 @@
3130
import org.springframework.messaging.MessageChannel;
3231
import org.springframework.messaging.MessageHandler;
3332
import org.springframework.messaging.SubscribableChannel;
33+
import org.springframework.messaging.simp.SimpLogging;
3434
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
3535
import org.springframework.messaging.simp.SimpMessageType;
3636
import org.springframework.messaging.support.ChannelInterceptor;
@@ -48,7 +48,7 @@
4848
public abstract class AbstractBrokerMessageHandler
4949
implements MessageHandler, ApplicationEventPublisherAware, SmartLifecycle {
5050

51-
protected final Log logger = LogFactory.getLog(getClass());
51+
protected final Log logger = SimpLogging.forLogName(getClass());
5252

5353
private final SubscribableChannel clientInboundChannel;
5454

spring-messaging/src/main/java/org/springframework/messaging/simp/broker/AbstractSubscriptionRegistry.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,10 @@
1717
package org.springframework.messaging.simp.broker;
1818

1919
import org.apache.commons.logging.Log;
20-
import org.apache.commons.logging.LogFactory;
2120

2221
import org.springframework.messaging.Message;
2322
import org.springframework.messaging.MessageHeaders;
23+
import org.springframework.messaging.simp.SimpLogging;
2424
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
2525
import org.springframework.messaging.simp.SimpMessageType;
2626
import org.springframework.util.CollectionUtils;
@@ -40,7 +40,7 @@ public abstract class AbstractSubscriptionRegistry implements SubscriptionRegist
4040
private static final MultiValueMap<String, String> EMPTY_MAP =
4141
CollectionUtils.unmodifiableMultiValueMap(new LinkedMultiValueMap<>(0));
4242

43-
protected final Log logger = LogFactory.getLog(getClass());
43+
protected final Log logger = SimpLogging.forLogName(getClass());
4444

4545

4646
@Override

spring-messaging/src/main/java/org/springframework/messaging/simp/config/AbstractMessageBrokerConfiguration.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@
3737
import org.springframework.messaging.converter.StringMessageConverter;
3838
import org.springframework.messaging.handler.invocation.HandlerMethodArgumentResolver;
3939
import org.springframework.messaging.handler.invocation.HandlerMethodReturnValueHandler;
40+
import org.springframework.messaging.simp.SimpLogging;
4041
import org.springframework.messaging.simp.SimpMessagingTemplate;
4142
import org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler;
4243
import org.springframework.messaging.simp.broker.AbstractBrokerMessageHandler;
@@ -125,6 +126,7 @@ public ApplicationContext getApplicationContext() {
125126
@Bean
126127
public AbstractSubscribableChannel clientInboundChannel() {
127128
ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(clientInboundChannelExecutor());
129+
channel.setLogger(SimpLogging.forLog(channel.getLogger()));
128130
ChannelRegistration reg = getClientInboundChannelRegistration();
129131
if (reg.hasInterceptors()) {
130132
channel.setInterceptors(reg.getInterceptors());
@@ -160,6 +162,7 @@ protected void configureClientInboundChannel(ChannelRegistration registration) {
160162
@Bean
161163
public AbstractSubscribableChannel clientOutboundChannel() {
162164
ExecutorSubscribableChannel channel = new ExecutorSubscribableChannel(clientOutboundChannelExecutor());
165+
channel.setLogger(SimpLogging.forLog(channel.getLogger()));
163166
ChannelRegistration reg = getClientOutboundChannelRegistration();
164167
if (reg.hasInterceptors()) {
165168
channel.setInterceptors(reg.getInterceptors());
@@ -198,6 +201,7 @@ public AbstractSubscribableChannel brokerChannel() {
198201
ExecutorSubscribableChannel channel = (reg.hasTaskExecutor() ?
199202
new ExecutorSubscribableChannel(brokerChannelExecutor()) : new ExecutorSubscribableChannel());
200203
reg.interceptors(new ImmutableMessageChannelInterceptor());
204+
channel.setLogger(SimpLogging.forLog(channel.getLogger()));
201205
channel.setInterceptors(reg.getInterceptors());
202206
return channel;
203207
}

spring-messaging/src/main/java/org/springframework/messaging/simp/stomp/DefaultStompSession.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@
2929
import java.util.concurrent.atomic.AtomicInteger;
3030

3131
import org.apache.commons.logging.Log;
32-
import org.apache.commons.logging.LogFactory;
3332

3433
import org.springframework.core.ResolvableType;
3534
import org.springframework.lang.Nullable;
@@ -38,6 +37,7 @@
3837
import org.springframework.messaging.converter.MessageConversionException;
3938
import org.springframework.messaging.converter.MessageConverter;
4039
import org.springframework.messaging.converter.SimpleMessageConverter;
40+
import org.springframework.messaging.simp.SimpLogging;
4141
import org.springframework.messaging.support.MessageBuilder;
4242
import org.springframework.messaging.support.MessageHeaderAccessor;
4343
import org.springframework.messaging.tcp.TcpConnection;
@@ -58,7 +58,7 @@
5858
*/
5959
public class DefaultStompSession implements ConnectionHandlingStompSession {
6060

61-
private static final Log logger = LogFactory.getLog(DefaultStompSession.class);
61+
private static final Log logger = SimpLogging.forLogName(DefaultStompSession.class);
6262

6363
private static final IdGenerator idGenerator = new AlternativeJdkIdGenerator();
6464

0 commit comments

Comments
 (0)