From 46e80d790eb7bc84e9b5b74043fcc02054fb212f Mon Sep 17 00:00:00 2001 From: Mark Fisher Date: Sat, 13 Aug 2011 14:05:54 -0400 Subject: [PATCH] Add 'loggerName' property. 1. LoggingHandler now has a loggerName property 2. The now has a 'logger-name' attribute Issue: INT-1947 --- .../xml/LoggingChannelAdapterParser.java | 3 +- .../integration/handler/LoggingHandler.java | 45 ++++++++++++------ .../config/xml/spring-integration-2.1.xsd | 9 ++++ ...ggingChannelAdapterParserTests-context.xml | 12 +++++ .../xml/LoggingChannelAdapterParserTests.java | 47 +++++++++++++++++++ .../handler/LoggingHandlerTests.java | 2 +- 6 files changed, 101 insertions(+), 17 deletions(-) create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/xml/LoggingChannelAdapterParserTests-context.xml create mode 100644 spring-integration-core/src/test/java/org/springframework/integration/config/xml/LoggingChannelAdapterParserTests.java diff --git a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/LoggingChannelAdapterParser.java b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/LoggingChannelAdapterParser.java index 18480c7901..b6ec7af884 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/config/xml/LoggingChannelAdapterParser.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/config/xml/LoggingChannelAdapterParser.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. @@ -46,6 +46,7 @@ public class LoggingChannelAdapterParser extends AbstractOutboundChannelAdapterP } builder.addPropertyValue("shouldLogFullMessage", logFullMessage); } + IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "logger-name"); IntegrationNamespaceUtils.setValueIfAttributeDefined(builder, element, "expression"); return builder.getBeanDefinition(); } diff --git a/spring-integration-core/src/main/java/org/springframework/integration/handler/LoggingHandler.java b/spring-integration-core/src/main/java/org/springframework/integration/handler/LoggingHandler.java index 475f04dcad..9df1922376 100644 --- a/spring-integration-core/src/main/java/org/springframework/integration/handler/LoggingHandler.java +++ b/spring-integration-core/src/main/java/org/springframework/integration/handler/LoggingHandler.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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. You may obtain a copy of the License at @@ -17,6 +17,9 @@ import java.io.PrintWriter; import java.io.StringWriter; import java.util.List; +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + import org.springframework.context.expression.MapAccessor; import org.springframework.expression.EvaluationContext; import org.springframework.expression.Expression; @@ -24,6 +27,7 @@ import org.springframework.expression.spel.standard.SpelExpressionParser; import org.springframework.expression.spel.support.StandardEvaluationContext; import org.springframework.integration.Message; import org.springframework.integration.dispatcher.AggregateMessageDeliveryException; +import org.springframework.util.Assert; import org.springframework.util.StringUtils; /** @@ -42,12 +46,16 @@ public class LoggingHandler extends AbstractMessageHandler { private static final SpelExpressionParser EXPRESSION_PARSER = new SpelExpressionParser(); + private volatile Expression expression; private final Level level; private final EvaluationContext evaluationContext; + private volatile Log messageLogger = this.logger; + + /** * Create a LoggingHandler with the given log level (case-insensitive). *

@@ -56,7 +64,8 @@ public class LoggingHandler extends AbstractMessageHandler { public LoggingHandler(String level) { try { this.level = Level.valueOf(level.toUpperCase()); - } catch (IllegalArgumentException e) { + } + catch (IllegalArgumentException e) { throw new IllegalArgumentException("Invalid log level '" + level + "'. The (case-insensitive) supported values are: " + StringUtils.arrayToCommaDelimitedString(Level.values())); @@ -66,11 +75,17 @@ public class LoggingHandler extends AbstractMessageHandler { this.evaluationContext = evaluationContext; this.expression = EXPRESSION_PARSER.parseExpression("payload"); } - + + public void setExpression(String expressionString) { this.expression = EXPRESSION_PARSER.parseExpression(expressionString); } + public void setLoggerName(String loggerName) { + Assert.hasText(loggerName, "loggerName must not be empty"); + this.messageLogger = LogFactory.getLog(loggerName); + } + /** * Specify whether to log the full Message. Otherwise, only the payload will be logged. This value is * false by default. @@ -102,33 +117,33 @@ public class LoggingHandler extends AbstractMessageHandler { } switch (this.level) { case FATAL: - if (logger.isFatalEnabled()) { - logger.fatal(logMessage); + if (messageLogger.isFatalEnabled()) { + messageLogger.fatal(logMessage); } break; case ERROR: - if (logger.isErrorEnabled()) { - logger.error(logMessage); + if (messageLogger.isErrorEnabled()) { + messageLogger.error(logMessage); } break; case WARN: - if (logger.isWarnEnabled()) { - logger.warn(logMessage); + if (messageLogger.isWarnEnabled()) { + messageLogger.warn(logMessage); } break; case INFO: - if (logger.isInfoEnabled()) { - logger.info(logMessage); + if (messageLogger.isInfoEnabled()) { + messageLogger.info(logMessage); } break; case DEBUG: - if (logger.isDebugEnabled()) { - logger.debug(logMessage); + if (messageLogger.isDebugEnabled()) { + messageLogger.debug(logMessage); } break; case TRACE: - if (logger.isTraceEnabled()) { - logger.trace(logMessage); + if (messageLogger.isTraceEnabled()) { + messageLogger.trace(logMessage); } break; } diff --git a/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.1.xsd b/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.1.xsd index 7eae5abd81..f8b97c1384 100644 --- a/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.1.xsd +++ b/spring-integration-core/src/main/resources/org/springframework/integration/config/xml/spring-integration-2.1.xsd @@ -752,6 +752,15 @@ endpoint itself is a Polling Consumer for a channel with a queue. + + + + Provide a name for the logger. This is useful when there are multiple logging Channel Adapters configured, + and you would like to differentiate them within the actual log. By default the logger name will be the + fully qualified class name of the LoggingHandler implementation. + + + + + + + + diff --git a/spring-integration-core/src/test/java/org/springframework/integration/config/xml/LoggingChannelAdapterParserTests.java b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/LoggingChannelAdapterParserTests.java new file mode 100644 index 0000000000..abbe3ba643 --- /dev/null +++ b/spring-integration-core/src/test/java/org/springframework/integration/config/xml/LoggingChannelAdapterParserTests.java @@ -0,0 +1,47 @@ +/* + * Copyright 2002-2011 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. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.integration.config.xml; + +import static org.junit.Assert.assertEquals; + +import org.junit.Test; +import org.junit.runner.RunWith; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.integration.handler.LoggingHandler; +import org.springframework.integration.test.util.TestUtils; +import org.springframework.test.context.ContextConfiguration; +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; + +/** + * @author Mark Fisher + * @since 2.1 + */ +@ContextConfiguration +@RunWith(SpringJUnit4ClassRunner.class) +public class LoggingChannelAdapterParserTests { + + @Autowired + private LoggingHandler loggingHandler; + + + @Test + public void verifyLoggerName() { + assertEquals("org.springframework.integration.test.logger", TestUtils.getPropertyValue(loggingHandler, "messageLogger.name")); + } + +} diff --git a/spring-integration-core/src/test/java/org/springframework/integration/handler/LoggingHandlerTests.java b/spring-integration-core/src/test/java/org/springframework/integration/handler/LoggingHandlerTests.java index 0be4db2ce6..0b92f9c571 100644 --- a/spring-integration-core/src/test/java/org/springframework/integration/handler/LoggingHandlerTests.java +++ b/spring-integration-core/src/test/java/org/springframework/integration/handler/LoggingHandlerTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2010 the original author or authors. + * Copyright 2002-2011 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.