You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ra...@apache.org on 2013/03/28 01:49:36 UTC
svn commit: r1461910 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/component/log/LogComponent.java
test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
Author: raulk
Date: Thu Mar 28 00:49:36 2013
New Revision: 1461910
URL: http://svn.apache.org/r1461910
Log:
CAMEL-5698 Allow to set a custom LogFormatter in the Log component
Added:
camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java?rev=1461910&r1=1461909&r2=1461910&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java Thu Mar 28 00:49:36 2013
@@ -25,6 +25,7 @@ import org.apache.camel.Processor;
import org.apache.camel.impl.DefaultComponent;
import org.apache.camel.processor.CamelLogProcessor;
import org.apache.camel.processor.ThroughputLogger;
+import org.apache.camel.spi.ExchangeFormatter;
import org.apache.camel.util.CamelLogger;
/**
@@ -35,6 +36,8 @@ import org.apache.camel.util.CamelLogger
*/
public class LogComponent extends DefaultComponent {
+ private ExchangeFormatter exchangeFormatter;
+
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters) throws Exception {
LoggingLevel level = getLoggingLevel(parameters);
String marker = getAndRemoveParameter(parameters, "marker", String.class);
@@ -50,10 +53,19 @@ public class LogComponent extends Defaul
Long groupDelay = getAndRemoveParameter(parameters, "groupDelay", Long.class);
logger = new ThroughputLogger(camelLogger, this.getCamelContext(), groupInterval, groupDelay, groupActiveOnly);
} else {
- LogFormatter formatter = new LogFormatter();
- setProperties(formatter, parameters);
-
- logger = new CamelLogProcessor(camelLogger, formatter);
+ // first, try to use the user-specified formatter (or the one picked up from the Registry and transferred to
+ // the property by a previous endpoint initialisation); if null, try to pick it up from the Registry now
+ ExchangeFormatter localFormatter = exchangeFormatter;
+ if (localFormatter == null) {
+ localFormatter = getCamelContext().getRegistry().lookupByNameAndType("logFormatter", ExchangeFormatter.class);
+ exchangeFormatter = localFormatter;
+ }
+ // if no formatter is available in the Registry, create a local one of the default type, for a single use
+ if (localFormatter == null) {
+ localFormatter = new LogFormatter();
+ setProperties(localFormatter, parameters);
+ }
+ logger = new CamelLogProcessor(camelLogger, localFormatter);
}
LogEndpoint endpoint = new LogEndpoint(uri, this);
@@ -69,4 +81,18 @@ public class LogComponent extends Defaul
return LoggingLevel.valueOf(levelText.toUpperCase(Locale.ENGLISH));
}
+ public ExchangeFormatter getExchangeFormatter() {
+ return exchangeFormatter;
+ }
+
+ /**
+ * Sets a custom {@link ExchangeFormatter} to convert the Exchange to a String suitable for logging.
+ * <p />
+ * If not specified, we default to {@link LogFormatter}.
+ * @param exchangeFormatter
+ */
+ public void setExchangeFormatter(ExchangeFormatter exchangeFormatter) {
+ this.exchangeFormatter = exchangeFormatter;
+ }
+
}
Added: camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java?rev=1461910&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogCustomFormatterTest.java Thu Mar 28 00:49:36 2013
@@ -0,0 +1,142 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.camel.component.log;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.impl.PropertyPlaceholderDelegateRegistry;
+import org.apache.camel.spi.ExchangeFormatter;
+import org.apache.log4j.ConsoleAppender;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.SimpleLayout;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Custom Exchange Formatter test.
+ */
+public class LogCustomFormatterTest extends ContextTestSupport {
+
+ private TestExchangeFormatter exchangeFormatter;
+
+ @Before @Override
+ public void setUp() throws Exception {
+ super.setUp();
+ // we add an appender explicitly to avoid getting a NOPLogger which permit logging;
+ // otherwise the ExchangeFormatter wouldn't get called
+ Logger.getLogger(LogCustomFormatterTest.class).removeAllAppenders();
+ Logger.getLogger(LogCustomFormatterTest.class).addAppender(new ConsoleAppender(new SimpleLayout()));
+ Logger.getLogger(LogCustomFormatterTest.class).setLevel(Level.TRACE);
+ }
+
+ @Test
+ public void testCustomFormatterInComponent() throws Exception {
+ context.stop();
+
+ LogComponent log = new LogComponent();
+ exchangeFormatter = new TestExchangeFormatter();
+ log.setExchangeFormatter(exchangeFormatter);
+ context.addComponent("log", log);
+
+ context.start();
+
+ String endpointUri = "log:" + LogCustomFormatterTest.class.getCanonicalName();
+ template.requestBody(endpointUri, "Hello World");
+ template.requestBody(endpointUri, "Hello World");
+ template.requestBody(endpointUri + "2", "Hello World");
+ template.requestBody(endpointUri + "2", "Hello World");
+
+ assertEquals(4, exchangeFormatter.getCounter());
+ }
+
+ @Test
+ public void testCustomFormatterInRegistry() throws Exception {
+ context.stop();
+
+ exchangeFormatter = new TestExchangeFormatter();
+ JndiRegistry registry = getRegistryAsJndi();
+ registry.bind("logFormatter", exchangeFormatter);
+
+ context.start();
+
+ String endpointUri = "log:" + LogCustomFormatterTest.class.getCanonicalName();
+ template.requestBody(endpointUri, "Hello World");
+ template.requestBody(endpointUri, "Hello World");
+ template.requestBody(endpointUri + "2", "Hello World");
+ template.requestBody(endpointUri + "2", "Hello World");
+
+ assertEquals(4, exchangeFormatter.getCounter());
+ }
+
+ @Test
+ public void testFormatterNotPickedUpWithDifferentKey() throws Exception {
+ context.stop();
+
+ exchangeFormatter = new TestExchangeFormatter();
+ JndiRegistry registry = getRegistryAsJndi();
+ registry.bind("anotherFormatter", exchangeFormatter);
+
+ context.start();
+
+ String endpointUri = "log:" + LogCustomFormatterTest.class.getCanonicalName();
+ template.requestBody(endpointUri, "Hello World");
+ template.requestBody(endpointUri, "Hello World");
+ template.requestBody(endpointUri + "2", "Hello World");
+ template.requestBody(endpointUri + "2", "Hello World");
+
+ assertEquals(0, exchangeFormatter.getCounter());
+ }
+
+ private JndiRegistry getRegistryAsJndi() {
+ JndiRegistry registry = null;
+ if (context.getRegistry() instanceof PropertyPlaceholderDelegateRegistry) {
+ registry = (JndiRegistry) ((PropertyPlaceholderDelegateRegistry) context.getRegistry()).getRegistry();
+ } else if (context.getRegistry() instanceof JndiRegistry) {
+ registry = (JndiRegistry) context.getRegistry();
+ } else {
+ fail("Could not determine Registry type");
+ }
+ return registry;
+ }
+
+ public static class TestExchangeFormatter implements ExchangeFormatter {
+ private int counter = 0;
+ private boolean addTen = false;
+
+ @Override
+ public String format(Exchange exchange) {
+ counter += addTen ? 10 : 1;
+ return exchange.toString();
+ }
+
+ public int getCounter() {
+ return counter;
+ }
+
+ public boolean isAddTen() {
+ return addTen;
+ }
+
+ public void setAddTen(boolean addTen) {
+ this.addTen = addTen;
+ }
+
+ }
+
+}