Add 'loggerName' property.

1. LoggingHandler now has a loggerName property
2. The <logging-channel-adapter> now has a 'logger-name' attribute

Issue: INT-1947
This commit is contained in:
Mark Fisher
2011-08-13 14:05:54 -04:00
parent 6d3eac8c49
commit 46e80d790e
6 changed files with 101 additions and 17 deletions

View File

@@ -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();
}

View File

@@ -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).
* <p>
@@ -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
* <code>false</code> 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;
}

View File

@@ -752,6 +752,15 @@ endpoint itself is a Polling Consumer for a channel with a queue.
</xsd:restriction>
</xsd:simpleType>
</xsd:attribute>
<xsd:attribute name="logger-name" type="xsd:string">
<xsd:annotation>
<xsd:documentation>
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.
</xsd:documentation>
</xsd:annotation>
</xsd:attribute>
<xsd:attribute name="expression">
<xsd:annotation>
<xsd:documentation><![CDATA[

View File

@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans:beans xmlns="http://www.springframework.org/schema/integration"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:beans="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/integration
http://www.springframework.org/schema/integration/spring-integration.xsd">
<logging-channel-adapter id="logger" logger-name="org.springframework.integration.test.logger"/>
</beans:beans>

View File

@@ -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"));
}
}

View File

@@ -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.