GH-3424: Refactor to use logging methods from LogAccessor

Fixes https://github.com/spring-projects/spring-integration/issues/3424

* Use `LogMessage.format()` for lazily formatting

* Fix some logging statements in `JdbcChannelMessageStore`
This commit is contained in:
trungPa
2021-07-19 22:30:01 +07:00
committed by GitHub
parent 39c3acb042
commit 9c718c37a6
30 changed files with 145 additions and 176 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.
@@ -20,6 +20,7 @@ import java.util.Iterator;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import org.springframework.core.log.LogMessage;
import org.springframework.integration.core.MessageSource;
import org.springframework.integration.handler.AbstractMessageHandler;
import org.springframework.integration.store.MessageGroup;
@@ -47,6 +48,7 @@ import org.springframework.messaging.Message;
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*
* @see AbstractCorrelatingMessageHandler
*/
@@ -93,9 +95,7 @@ public class CorrelatingMessageBarrier extends AbstractMessageHandler implements
synchronized (lock) {
this.store.addMessagesToGroup(correlationKey, message);
}
if (logger.isDebugEnabled()) {
logger.debug(String.format("Handled message for key [%s]: %s.", correlationKey, message));
}
logger.debug(LogMessage.format("Handled message for key [%s]: %s.", correlationKey, message));
}
private Object getLock(Object correlationKey) {
@@ -119,9 +119,7 @@ public class CorrelatingMessageBarrier extends AbstractMessageHandler implements
if (messages.hasNext()) {
nextMessage = messages.next();
this.store.removeMessagesFromGroup(key, nextMessage);
if (logger.isDebugEnabled()) {
logger.debug(String.format("Released message for key [%s]: %s.", key, nextMessage));
}
logger.debug(LogMessage.format("Released message for key [%s]: %s.", key, nextMessage));
}
else {
remove(key);

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.
@@ -20,6 +20,7 @@ import java.util.ArrayDeque;
import java.util.Deque;
import java.util.List;
import org.springframework.core.log.LogMessage;
import org.springframework.integration.IntegrationPatternType;
import org.springframework.integration.support.management.metrics.CounterFacade;
import org.springframework.integration.support.management.metrics.MetricsCaptor;
@@ -36,6 +37,7 @@ import org.springframework.messaging.support.ExecutorChannelInterceptor;
* @author Artem Bilan
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*/
public abstract class AbstractPollableChannel extends AbstractMessageChannel
implements PollableChannel, ExecutorChannelInterceptorAware {
@@ -99,10 +101,7 @@ public abstract class AbstractPollableChannel extends AbstractMessageChannel
else {
incrementReceiveCounter();
counted = true;
if (logger.isDebugEnabled()) {
logger.debug("postReceive on channel '" + this + "', message: " + message);
}
logger.debug(LogMessage.format("postReceive on channel '%s', message: %s", this, message));
}
if (interceptorStack != null && message != null) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2013-2020 the original author or authors.
* Copyright 2013-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.
@@ -43,6 +43,7 @@ import org.springframework.util.Assert;
*
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*
* @since 3.0
*
@@ -166,9 +167,7 @@ public class DefaultHeaderChannelRegistry extends IntegrationObjectSupport
String name = this.uuid + id.incrementAndGet();
this.channels.put(name, new MessageChannelWrapper((MessageChannel) channel,
System.currentTimeMillis() + timeToLive));
if (logger.isDebugEnabled()) {
logger.debug("Registered " + channel + " as " + name);
}
logger.debug(() -> "Registered " + channel + " as " + name);
return name;
}
else {
@@ -187,8 +186,8 @@ public class DefaultHeaderChannelRegistry extends IntegrationObjectSupport
else {
messageChannelWrapper = this.channels.get(name);
}
if (logger.isDebugEnabled() && messageChannelWrapper != null) {
logger.debug("Retrieved " + messageChannelWrapper.getChannel() + " with " + name);
if (messageChannelWrapper != null) {
logger.debug(() -> "Retrieved " + messageChannelWrapper.getChannel() + " with " + name);
}
return messageChannelWrapper == null ? null : messageChannelWrapper.getChannel();
@@ -211,17 +210,13 @@ public class DefaultHeaderChannelRegistry extends IntegrationObjectSupport
@Override
public synchronized void run() {
if (logger.isTraceEnabled()) {
logger.trace("Reaper started; channels size=" + this.channels.size());
}
logger.trace(() -> "Reaper started; channels size=" + this.channels.size());
Iterator<Entry<String, MessageChannelWrapper>> iterator = this.channels.entrySet().iterator();
long now = System.currentTimeMillis();
while (iterator.hasNext()) {
Entry<String, MessageChannelWrapper> entry = iterator.next();
if (entry.getValue().getExpireAt() < now) {
if (logger.isDebugEnabled()) {
logger.debug("Expiring " + entry.getKey() + " (" + entry.getValue().getChannel() + ")");
}
logger.debug(() -> "Expiring " + entry.getKey() + " (" + entry.getValue().getChannel() + ")");
iterator.remove();
}
}
@@ -229,9 +224,7 @@ public class DefaultHeaderChannelRegistry extends IntegrationObjectSupport
getTaskScheduler()
.schedule(this, new Date(System.currentTimeMillis() + this.reaperDelay));
if (logger.isTraceEnabled()) {
logger.trace("Reaper completed; channels size=" + this.channels.size());
}
logger.trace(() -> "Reaper completed; channels size=" + this.channels.size());
}

View File

@@ -34,6 +34,7 @@ import org.springframework.util.ErrorHandler;
* @author Oleg Zhurakousky
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*/
public class PublishSubscribeChannel extends AbstractExecutorChannel implements BroadcastCapableChannel {
@@ -180,8 +181,8 @@ public class PublishSubscribeChannel extends AbstractExecutorChannel implements
dispatcherToUse.setMinSubscribers(this.minSubscribers);
this.dispatcher = dispatcherToUse;
}
else if (this.errorHandler != null && this.logger.isWarnEnabled()) {
this.logger.warn("The 'errorHandler' is ignored for the '" + getComponentName() +
else if (this.errorHandler != null) {
this.logger.warn(() -> "The 'errorHandler' is ignored for the '" + getComponentName() +
"' (an 'executor' is not provided) and exceptions will be thrown " +
"directly within the sending Thread");
}

View File

@@ -44,6 +44,7 @@ import org.springframework.util.StringUtils;
* @author Kris Jacyna
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*/
@IntegrationManagedResource
public abstract class AbstractEndpoint extends IntegrationObjectSupport
@@ -155,9 +156,7 @@ public abstract class AbstractEndpoint extends IntegrationObjectSupport
this.active = true;
doStart();
this.running = true;
if (logger.isInfoEnabled()) {
logger.info("started " + this);
}
logger.info(() -> "started " + this);
}
}
finally {
@@ -173,9 +172,7 @@ public abstract class AbstractEndpoint extends IntegrationObjectSupport
this.active = false;
doStop();
this.running = false;
if (logger.isInfoEnabled()) {
logger.info("stopped " + this);
}
logger.info(() -> "stopped " + this);
}
}
finally {
@@ -191,9 +188,7 @@ public abstract class AbstractEndpoint extends IntegrationObjectSupport
this.active = false;
doStop(callback);
this.running = false;
if (logger.isInfoEnabled()) {
logger.info("stopped " + this);
}
logger.info(() -> "stopped " + this);
}
else {
callback.run();

View File

@@ -49,6 +49,7 @@ import reactor.core.publisher.Flux;
* input channel and reactive consumption of messages from that channel.
*
* @author Artem Bilan
* @author Trung Pham
*
* @since 5.0
*/
@@ -89,7 +90,7 @@ public class ReactiveStreamsConsumer extends AbstractEndpoint implements Integra
Assert.notNull(subscriber, "'subscriber' must not be null");
this.inputChannel = inputChannel;
if (inputChannel instanceof NullChannel && logger.isWarnEnabled()) {
if (inputChannel instanceof NullChannel) {
logger.warn("The consuming from the NullChannel does not have any effects: " +
"it doesn't forward messages sent to it. A NullChannel is the end of the flow.");
}

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.
@@ -78,6 +78,7 @@ import reactor.core.publisher.Sinks;
* @author Mark Fisher
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*/
@IntegrationManagedResource
public abstract class MessagingGatewaySupport extends AbstractEndpoint
@@ -529,9 +530,7 @@ public abstract class MessagingGatewaySupport extends AbstractEndpoint
}
}
catch (Throwable ex) { // NOSONAR (catch throwable)
if (logger.isDebugEnabled()) {
logger.debug("failure occurred in gateway sendAndReceive: " + ex.getMessage());
}
logger.debug(() -> "failure occurred in gateway sendAndReceive: " + ex.getMessage());
reply = ex;
if (sample != null) {
sample.stop(buildSendTimer(false, ex.getClass().getSimpleName()));
@@ -708,9 +707,7 @@ public abstract class MessagingGatewaySupport extends AbstractEndpoint
}
private Mono<Message<?>> handleSendError(Message<?> requestMessage, Throwable exception) {
if (logger.isDebugEnabled()) {
logger.debug("failure occurred in gateway sendAndReceiveReactive: " + exception.getMessage());
}
logger.debug(() -> "failure occurred in gateway sendAndReceiveReactive: " + exception.getMessage());
MessageChannel channel = getErrorChannel();
if (channel != null) {
ErrorMessage errorMessage = buildErrorMessage(requestMessage, exception);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2019-2020 the original author or authors.
* Copyright 2019-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.
@@ -16,6 +16,7 @@
package org.springframework.integration.handler;
import org.springframework.core.log.LogMessage;
import org.springframework.integration.history.MessageHistory;
import org.springframework.messaging.Message;
import org.springframework.messaging.ReactiveMessageHandler;
@@ -28,6 +29,7 @@ import reactor.core.publisher.Mono;
*
* @author David Turanski
* @author Artem Bilan
* @author Trung Pham
*
* @since 5.3
*/
@@ -37,8 +39,8 @@ public abstract class AbstractReactiveMessageHandler extends MessageHandlerSuppo
@Override
public Mono<Void> handleMessage(final Message<?> message) {
Assert.notNull(message, "message must not be null");
if (isLoggingEnabled() && this.logger.isDebugEnabled()) {
this.logger.debug(this + " received message: " + message);
if (isLoggingEnabled()) {
this.logger.debug(LogMessage.format("%s received message: %s", this, message));
}
final Message<?> messageToUse;
@@ -49,8 +51,8 @@ public abstract class AbstractReactiveMessageHandler extends MessageHandlerSuppo
messageToUse = message;
}
return handleMessageInternal(messageToUse)
.doOnError((ex) -> this.logger.error(ex, () ->
"An error occurred in message handler [" + this + "] on message [" + messageToUse + "]"));
.doOnError((ex) -> this.logger.error(ex,
LogMessage.format("An error occurred in message handler [%s] on message [%s]", this, messageToUse)));
}
protected abstract Mono<Void> handleMessageInternal(Message<?> message);

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.
@@ -23,6 +23,7 @@ import org.aopalliance.aop.Advice;
import org.springframework.aop.framework.ProxyFactory;
import org.springframework.beans.factory.BeanClassLoaderAware;
import org.springframework.core.log.LogMessage;
import org.springframework.integration.IntegrationPatternType;
import org.springframework.integration.handler.advice.HandleMessageAdvice;
import org.springframework.lang.Nullable;
@@ -39,6 +40,7 @@ import org.springframework.util.ClassUtils;
* @author Gary Russell
* @author Artem Bilan
* @author David Liu
* @author Trung Pham
*/
public abstract class AbstractReplyProducingMessageHandler extends AbstractMessageProducingHandler
implements BeanClassLoaderAware {
@@ -143,8 +145,8 @@ public abstract class AbstractReplyProducingMessageHandler extends AbstractMessa
throw new ReplyRequiredException(message, "No reply produced by handler '" +
getComponentName() + "', and its 'requiresReply' property is set to true.");
}
else if (!isAsync() && logger.isDebugEnabled()) {
logger.debug("handler '" + this + "' produced no reply for request Message: " + message);
else if (!isAsync()) {
logger.debug(LogMessage.format("handler '%s' produced no reply for request Message: %s", this, message));
}
}

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.
@@ -65,6 +65,7 @@ import org.springframework.util.Assert;
* @author Iwein Fuld
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*/
public class MessageHandlerChain extends AbstractMessageProducingHandler
implements CompositeMessageHandler, ManageableLifecycle {
@@ -182,9 +183,7 @@ public class MessageHandlerChain extends AbstractMessageProducingHandler
if (!this.running) {
doStart();
this.running = true;
if (logger.isInfoEnabled()) {
logger.info("started " + this);
}
logger.info(() -> "started " + this);
}
}
finally {
@@ -199,9 +198,7 @@ public class MessageHandlerChain extends AbstractMessageProducingHandler
if (this.running) {
doStop();
this.running = false;
if (logger.isInfoEnabled()) {
logger.info("stopped " + this);
}
logger.info(() -> "stopped " + this);
}
}
finally {

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.
@@ -30,6 +30,7 @@ import org.springframework.messaging.MessagingException;
*
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*
* @since 2.2
*
@@ -73,8 +74,8 @@ public class RequestHandlerCircuitBreakerAdvice extends AbstractRequestHandlerAd
}
try {
Object result = callback.execute();
if (logger.isDebugEnabled() && metadata.getFailures().get() > 0) {
logger.debug("Closing Circuit Breaker for " + target);
if (metadata.getFailures().get() > 0) {
logger.debug(() -> "Closing Circuit Breaker for " + target);
}
metadata.getFailures().set(0);
return result;

View File

@@ -118,6 +118,7 @@ import org.springframework.util.StringUtils;
* @author Soby Chacko
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*
* @since 2.0
*/
@@ -830,8 +831,8 @@ public class MessagingMethodInvokerHelper extends AbstractExpressionEvaluator im
|| Lifecycle.class.isAssignableFrom(declaringClass))
&& ReflectionUtils.findMethod(Pausable.class, pausableMethod.getName(),
pausableMethod.getParameterTypes()) != null;
if (pausable && this.logger.isTraceEnabled()) {
this.logger.trace(pausableMethod + " is not considered a candidate method unless explicitly requested");
if (pausable) {
this.logger.trace(() -> pausableMethod + " is not considered a candidate method unless explicitly requested");
}
return pausable;
}

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.
@@ -28,6 +28,7 @@ import java.util.Properties;
import java.util.Set;
import org.springframework.core.convert.ConversionService;
import org.springframework.core.log.LogMessage;
import org.springframework.integration.support.management.MappingMessageRouterManagement;
import org.springframework.jmx.export.annotation.ManagedAttribute;
import org.springframework.jmx.export.annotation.ManagedOperation;
@@ -47,6 +48,7 @@ import org.springframework.util.StringUtils;
* @author Gunnar Hillert
* @author Gary Russell
* @author Artem Bilan
* @author Trung Pham
*
* @since 2.1
*/
@@ -230,9 +232,7 @@ public abstract class AbstractMappingMessageRouter extends AbstractMessageRouter
private void doSetChannelMappings(Map<String, String> newChannelMappings) {
Map<String, String> oldChannelMappings = this.channelMappings;
this.channelMappings = newChannelMappings;
if (logger.isDebugEnabled()) {
logger.debug("Channel mappings: " + oldChannelMappings + " replaced with: " + newChannelMappings);
}
logger.debug(LogMessage.format("Channel mappings: %s replaced with: %s", oldChannelMappings, newChannelMappings));
}
private MessageChannel resolveChannelForName(String channelName, Message<?> message) {

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.
@@ -18,6 +18,7 @@ package org.springframework.integration.transformer;
import java.util.UUID;
import org.springframework.core.log.LogMessage;
import org.springframework.integration.IntegrationPattern;
import org.springframework.integration.IntegrationPatternType;
import org.springframework.integration.store.MessageStore;
@@ -34,6 +35,7 @@ import org.springframework.util.Assert;
* @author Oleg Zhurakousky
* @author Nick Spacek
* @author Artem Bilan
* @author Trung Pham
*
* @since 2.0
*/
@@ -76,9 +78,7 @@ public class ClaimCheckOutTransformer extends AbstractTransformer implements Int
Message<?> retrievedMessage;
if (this.removeMessage) {
retrievedMessage = this.messageStore.removeMessage(id);
if (logger.isDebugEnabled()) {
logger.debug("Removed Message with claim-check '" + id + "' from the MessageStore.");
}
logger.debug(LogMessage.format("Removed Message with claim-check '%s' from the MessageStore.", id));
}
else {
retrievedMessage = this.messageStore.getMessage(id);

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.
@@ -43,6 +43,7 @@ import org.springframework.messaging.MessageHeaders;
* @author David Turanski
* @author Artem Bilan
* @author Gary Russell
* @author Trung Pham
*/
public class HeaderEnricher extends IntegrationObjectSupport implements Transformer, IntegrationPattern {
@@ -113,8 +114,8 @@ public class HeaderEnricher extends IntegrationObjectSupport implements Transfor
((BeanFactoryAware) this.messageProcessor).setBeanFactory(beanFactory);
}
if (!shouldOverwrite && !this.shouldSkipNulls && logger.isWarnEnabled()) {
logger.warn(getComponentName() +
if (!shouldOverwrite && !this.shouldSkipNulls) {
logger.warn(() -> getComponentName() +
" is configured to not overwrite existing headers. 'shouldSkipNulls = false' will have no effect");
}
}
@@ -192,13 +193,13 @@ public class HeaderEnricher extends IntegrationObjectSupport implements Transfor
messageBuilder.setHeader((String) key, entry.getValue());
}
}
else if (logger.isDebugEnabled()) {
logger.debug("ignoring value for non-String key: " + key);
else {
logger.debug(() -> "ignoring value for non-String key: " + key);
}
}
}
else if (logger.isDebugEnabled()) {
logger.debug("expected a Map result from processor, but received: " + result);
else {
logger.debug(() -> "expected a Map result from processor, but received: " + result);
}
}
}