Skip to content

Commit 9c718c3

Browse files
authored
GH-3424: Refactor to use logging methods from LogAccessor
Fixes #3424 * Use `LogMessage.format()` for lazily formatting * Fix some logging statements in `JdbcChannelMessageStore`
1 parent 39c3acb commit 9c718c3

File tree

30 files changed

+145
-176
lines changed

30 files changed

+145
-176
lines changed

spring-integration-core/src/main/java/org/springframework/integration/aggregator/CorrelatingMessageBarrier.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -20,6 +20,7 @@
2020
import java.util.concurrent.ConcurrentHashMap;
2121
import java.util.concurrent.ConcurrentMap;
2222

23+
import org.springframework.core.log.LogMessage;
2324
import org.springframework.integration.core.MessageSource;
2425
import org.springframework.integration.handler.AbstractMessageHandler;
2526
import org.springframework.integration.store.MessageGroup;
@@ -47,6 +48,7 @@
4748
* @author Oleg Zhurakousky
4849
* @author Gary Russell
4950
* @author Artem Bilan
51+
* @author Trung Pham
5052
*
5153
* @see AbstractCorrelatingMessageHandler
5254
*/
@@ -93,9 +95,7 @@ protected void handleMessageInternal(Message<?> message) {
9395
synchronized (lock) {
9496
this.store.addMessagesToGroup(correlationKey, message);
9597
}
96-
if (logger.isDebugEnabled()) {
97-
logger.debug(String.format("Handled message for key [%s]: %s.", correlationKey, message));
98-
}
98+
logger.debug(LogMessage.format("Handled message for key [%s]: %s.", correlationKey, message));
9999
}
100100

101101
private Object getLock(Object correlationKey) {
@@ -119,9 +119,7 @@ public Message<Object> receive() {
119119
if (messages.hasNext()) {
120120
nextMessage = messages.next();
121121
this.store.removeMessagesFromGroup(key, nextMessage);
122-
if (logger.isDebugEnabled()) {
123-
logger.debug(String.format("Released message for key [%s]: %s.", key, nextMessage));
124-
}
122+
logger.debug(LogMessage.format("Released message for key [%s]: %s.", key, nextMessage));
125123
}
126124
else {
127125
remove(key);

spring-integration-core/src/main/java/org/springframework/integration/channel/AbstractPollableChannel.java

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -20,6 +20,7 @@
2020
import java.util.Deque;
2121
import java.util.List;
2222

23+
import org.springframework.core.log.LogMessage;
2324
import org.springframework.integration.IntegrationPatternType;
2425
import org.springframework.integration.support.management.metrics.CounterFacade;
2526
import org.springframework.integration.support.management.metrics.MetricsCaptor;
@@ -36,6 +37,7 @@
3637
* @author Artem Bilan
3738
* @author Gary Russell
3839
* @author Artem Bilan
40+
* @author Trung Pham
3941
*/
4042
public abstract class AbstractPollableChannel extends AbstractMessageChannel
4143
implements PollableChannel, ExecutorChannelInterceptorAware {
@@ -99,10 +101,7 @@ public Message<?> receive(long timeout) {
99101
else {
100102
incrementReceiveCounter();
101103
counted = true;
102-
103-
if (logger.isDebugEnabled()) {
104-
logger.debug("postReceive on channel '" + this + "', message: " + message);
105-
}
104+
logger.debug(LogMessage.format("postReceive on channel '%s', message: %s", this, message));
106105
}
107106

108107
if (interceptorStack != null && message != null) {

spring-integration-core/src/main/java/org/springframework/integration/channel/DefaultHeaderChannelRegistry.java

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2013-2020 the original author or authors.
2+
* Copyright 2013-2021 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.
@@ -43,6 +43,7 @@
4343
*
4444
* @author Gary Russell
4545
* @author Artem Bilan
46+
* @author Trung Pham
4647
*
4748
* @since 3.0
4849
*
@@ -166,9 +167,7 @@ public Object channelToChannelName(@Nullable Object channel, long timeToLive) {
166167
String name = this.uuid + id.incrementAndGet();
167168
this.channels.put(name, new MessageChannelWrapper((MessageChannel) channel,
168169
System.currentTimeMillis() + timeToLive));
169-
if (logger.isDebugEnabled()) {
170-
logger.debug("Registered " + channel + " as " + name);
171-
}
170+
logger.debug(() -> "Registered " + channel + " as " + name);
172171
return name;
173172
}
174173
else {
@@ -187,8 +186,8 @@ public MessageChannel channelNameToChannel(@Nullable String name) {
187186
else {
188187
messageChannelWrapper = this.channels.get(name);
189188
}
190-
if (logger.isDebugEnabled() && messageChannelWrapper != null) {
191-
logger.debug("Retrieved " + messageChannelWrapper.getChannel() + " with " + name);
189+
if (messageChannelWrapper != null) {
190+
logger.debug(() -> "Retrieved " + messageChannelWrapper.getChannel() + " with " + name);
192191
}
193192

194193
return messageChannelWrapper == null ? null : messageChannelWrapper.getChannel();
@@ -211,27 +210,21 @@ public synchronized void runReaper() {
211210

212211
@Override
213212
public synchronized void run() {
214-
if (logger.isTraceEnabled()) {
215-
logger.trace("Reaper started; channels size=" + this.channels.size());
216-
}
213+
logger.trace(() -> "Reaper started; channels size=" + this.channels.size());
217214
Iterator<Entry<String, MessageChannelWrapper>> iterator = this.channels.entrySet().iterator();
218215
long now = System.currentTimeMillis();
219216
while (iterator.hasNext()) {
220217
Entry<String, MessageChannelWrapper> entry = iterator.next();
221218
if (entry.getValue().getExpireAt() < now) {
222-
if (logger.isDebugEnabled()) {
223-
logger.debug("Expiring " + entry.getKey() + " (" + entry.getValue().getChannel() + ")");
224-
}
219+
logger.debug(() -> "Expiring " + entry.getKey() + " (" + entry.getValue().getChannel() + ")");
225220
iterator.remove();
226221
}
227222
}
228223
this.reaperScheduledFuture =
229224
getTaskScheduler()
230225
.schedule(this, new Date(System.currentTimeMillis() + this.reaperDelay));
231226

232-
if (logger.isTraceEnabled()) {
233-
logger.trace("Reaper completed; channels size=" + this.channels.size());
234-
}
227+
logger.trace(() -> "Reaper completed; channels size=" + this.channels.size());
235228
}
236229

237230

spring-integration-core/src/main/java/org/springframework/integration/channel/PublishSubscribeChannel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
* @author Oleg Zhurakousky
3535
* @author Gary Russell
3636
* @author Artem Bilan
37+
* @author Trung Pham
3738
*/
3839
public class PublishSubscribeChannel extends AbstractExecutorChannel implements BroadcastCapableChannel {
3940

@@ -180,8 +181,8 @@ public final void onInit() {
180181
dispatcherToUse.setMinSubscribers(this.minSubscribers);
181182
this.dispatcher = dispatcherToUse;
182183
}
183-
else if (this.errorHandler != null && this.logger.isWarnEnabled()) {
184-
this.logger.warn("The 'errorHandler' is ignored for the '" + getComponentName() +
184+
else if (this.errorHandler != null) {
185+
this.logger.warn(() -> "The 'errorHandler' is ignored for the '" + getComponentName() +
185186
"' (an 'executor' is not provided) and exceptions will be thrown " +
186187
"directly within the sending Thread");
187188
}

spring-integration-core/src/main/java/org/springframework/integration/endpoint/AbstractEndpoint.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
* @author Kris Jacyna
4545
* @author Gary Russell
4646
* @author Artem Bilan
47+
* @author Trung Pham
4748
*/
4849
@IntegrationManagedResource
4950
public abstract class AbstractEndpoint extends IntegrationObjectSupport
@@ -155,9 +156,7 @@ public final void start() {
155156
this.active = true;
156157
doStart();
157158
this.running = true;
158-
if (logger.isInfoEnabled()) {
159-
logger.info("started " + this);
160-
}
159+
logger.info(() -> "started " + this);
161160
}
162161
}
163162
finally {
@@ -173,9 +172,7 @@ public final void stop() {
173172
this.active = false;
174173
doStop();
175174
this.running = false;
176-
if (logger.isInfoEnabled()) {
177-
logger.info("stopped " + this);
178-
}
175+
logger.info(() -> "stopped " + this);
179176
}
180177
}
181178
finally {
@@ -191,9 +188,7 @@ public final void stop(Runnable callback) {
191188
this.active = false;
192189
doStop(callback);
193190
this.running = false;
194-
if (logger.isInfoEnabled()) {
195-
logger.info("stopped " + this);
196-
}
191+
logger.info(() -> "stopped " + this);
197192
}
198193
else {
199194
callback.run();

spring-integration-core/src/main/java/org/springframework/integration/endpoint/ReactiveStreamsConsumer.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@
4949
* input channel and reactive consumption of messages from that channel.
5050
*
5151
* @author Artem Bilan
52+
* @author Trung Pham
5253
*
5354
* @since 5.0
5455
*/
@@ -89,7 +90,7 @@ public ReactiveStreamsConsumer(MessageChannel inputChannel, Subscriber<Message<?
8990
Assert.notNull(subscriber, "'subscriber' must not be null");
9091
this.inputChannel = inputChannel;
9192

92-
if (inputChannel instanceof NullChannel && logger.isWarnEnabled()) {
93+
if (inputChannel instanceof NullChannel) {
9394
logger.warn("The consuming from the NullChannel does not have any effects: " +
9495
"it doesn't forward messages sent to it. A NullChannel is the end of the flow.");
9596
}

spring-integration-core/src/main/java/org/springframework/integration/gateway/MessagingGatewaySupport.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -78,6 +78,7 @@
7878
* @author Mark Fisher
7979
* @author Gary Russell
8080
* @author Artem Bilan
81+
* @author Trung Pham
8182
*/
8283
@IntegrationManagedResource
8384
public abstract class MessagingGatewaySupport extends AbstractEndpoint
@@ -529,9 +530,7 @@ private Object doSendAndReceive(Object object, boolean shouldConvert) {
529530
}
530531
}
531532
catch (Throwable ex) { // NOSONAR (catch throwable)
532-
if (logger.isDebugEnabled()) {
533-
logger.debug("failure occurred in gateway sendAndReceive: " + ex.getMessage());
534-
}
533+
logger.debug(() -> "failure occurred in gateway sendAndReceive: " + ex.getMessage());
535534
reply = ex;
536535
if (sample != null) {
537536
sample.stop(buildSendTimer(false, ex.getClass().getSimpleName()));
@@ -708,9 +707,7 @@ private Mono<Message<?>> buildReplyMono(Message<?> requestMessage, Mono<Message<
708707
}
709708

710709
private Mono<Message<?>> handleSendError(Message<?> requestMessage, Throwable exception) {
711-
if (logger.isDebugEnabled()) {
712-
logger.debug("failure occurred in gateway sendAndReceiveReactive: " + exception.getMessage());
713-
}
710+
logger.debug(() -> "failure occurred in gateway sendAndReceiveReactive: " + exception.getMessage());
714711
MessageChannel channel = getErrorChannel();
715712
if (channel != null) {
716713
ErrorMessage errorMessage = buildErrorMessage(requestMessage, exception);

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractReactiveMessageHandler.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2020 the original author or authors.
2+
* Copyright 2019-2021 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.
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.integration.handler;
1818

19+
import org.springframework.core.log.LogMessage;
1920
import org.springframework.integration.history.MessageHistory;
2021
import org.springframework.messaging.Message;
2122
import org.springframework.messaging.ReactiveMessageHandler;
@@ -28,6 +29,7 @@
2829
*
2930
* @author David Turanski
3031
* @author Artem Bilan
32+
* @author Trung Pham
3133
*
3234
* @since 5.3
3335
*/
@@ -37,8 +39,8 @@ public abstract class AbstractReactiveMessageHandler extends MessageHandlerSuppo
3739
@Override
3840
public Mono<Void> handleMessage(final Message<?> message) {
3941
Assert.notNull(message, "message must not be null");
40-
if (isLoggingEnabled() && this.logger.isDebugEnabled()) {
41-
this.logger.debug(this + " received message: " + message);
42+
if (isLoggingEnabled()) {
43+
this.logger.debug(LogMessage.format("%s received message: %s", this, message));
4244
}
4345

4446
final Message<?> messageToUse;
@@ -49,8 +51,8 @@ public Mono<Void> handleMessage(final Message<?> message) {
4951
messageToUse = message;
5052
}
5153
return handleMessageInternal(messageToUse)
52-
.doOnError((ex) -> this.logger.error(ex, () ->
53-
"An error occurred in message handler [" + this + "] on message [" + messageToUse + "]"));
54+
.doOnError((ex) -> this.logger.error(ex,
55+
LogMessage.format("An error occurred in message handler [%s] on message [%s]", this, messageToUse)));
5456
}
5557

5658
protected abstract Mono<Void> handleMessageInternal(Message<?> message);

spring-integration-core/src/main/java/org/springframework/integration/handler/AbstractReplyProducingMessageHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -23,6 +23,7 @@
2323

2424
import org.springframework.aop.framework.ProxyFactory;
2525
import org.springframework.beans.factory.BeanClassLoaderAware;
26+
import org.springframework.core.log.LogMessage;
2627
import org.springframework.integration.IntegrationPatternType;
2728
import org.springframework.integration.handler.advice.HandleMessageAdvice;
2829
import org.springframework.lang.Nullable;
@@ -39,6 +40,7 @@
3940
* @author Gary Russell
4041
* @author Artem Bilan
4142
* @author David Liu
43+
* @author Trung Pham
4244
*/
4345
public abstract class AbstractReplyProducingMessageHandler extends AbstractMessageProducingHandler
4446
implements BeanClassLoaderAware {
@@ -143,8 +145,8 @@ else if (this.requiresReply && !isAsync()) {
143145
throw new ReplyRequiredException(message, "No reply produced by handler '" +
144146
getComponentName() + "', and its 'requiresReply' property is set to true.");
145147
}
146-
else if (!isAsync() && logger.isDebugEnabled()) {
147-
logger.debug("handler '" + this + "' produced no reply for request Message: " + message);
148+
else if (!isAsync()) {
149+
logger.debug(LogMessage.format("handler '%s' produced no reply for request Message: %s", this, message));
148150
}
149151
}
150152

spring-integration-core/src/main/java/org/springframework/integration/handler/MessageHandlerChain.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 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.
@@ -65,6 +65,7 @@
6565
* @author Iwein Fuld
6666
* @author Gary Russell
6767
* @author Artem Bilan
68+
* @author Trung Pham
6869
*/
6970
public class MessageHandlerChain extends AbstractMessageProducingHandler
7071
implements CompositeMessageHandler, ManageableLifecycle {
@@ -182,9 +183,7 @@ public final void start() {
182183
if (!this.running) {
183184
doStart();
184185
this.running = true;
185-
if (logger.isInfoEnabled()) {
186-
logger.info("started " + this);
187-
}
186+
logger.info(() -> "started " + this);
188187
}
189188
}
190189
finally {
@@ -199,9 +198,7 @@ public final void stop() {
199198
if (this.running) {
200199
doStop();
201200
this.running = false;
202-
if (logger.isInfoEnabled()) {
203-
logger.info("stopped " + this);
204-
}
201+
logger.info(() -> "stopped " + this);
205202
}
206203
}
207204
finally {

0 commit comments

Comments
 (0)