camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [10/13] git commit: [CAMEL-6694] Allow user to pass logger instance in camel-log endpoint URI
Date Fri, 04 Apr 2014 06:36:55 GMT
[CAMEL-6694] Allow user to pass logger instance in camel-log endpoint
URI

The URI has form: log:logger-name?level=info&logger=#mylogger

"mylogger" is registered in context's registry


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/043449cb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/043449cb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/043449cb

Branch: refs/heads/camel-2.12.x
Commit: 043449cb2cc1453471adebd9bc15205732764ee9
Parents: 773e5da
Author: Grzegorz Grzybek <gr.grzybek@gmail.com>
Authored: Tue Apr 1 12:06:36 2014 +0200
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Fri Apr 4 08:39:40 2014 +0200

----------------------------------------------------------------------
 .../camel/component/log/LogComponent.java       | 20 +++++++-
 .../camel/itest/osgi/core/log/LogRouteTest.java | 48 +++++++++++++++++++-
 .../src/test/resources/log4j.properties         | 13 ++++++
 3 files changed, 79 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/043449cb/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java b/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
index 250d745..b3dc3a7 100644
--- a/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
+++ b/camel-core/src/main/java/org/apache/camel/component/log/LogComponent.java
@@ -28,6 +28,7 @@ import org.apache.camel.processor.DefaultExchangeFormatter;
 import org.apache.camel.processor.ThroughputLogger;
 import org.apache.camel.spi.ExchangeFormatter;
 import org.apache.camel.util.CamelLogger;
+import org.slf4j.Logger;
 
 /**
  * The <a href="http://camel.apache.org/log.html">Log Component</a>
@@ -45,12 +46,18 @@ public class LogComponent extends UriEndpointComponent {
     
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
         LoggingLevel level = getLoggingLevel(parameters);
+        Logger providedLogger = getLogger(parameters);
 
         LogEndpoint endpoint = new LogEndpoint(uri, this);
         endpoint.setLevel(level.name());
         setProperties(endpoint, parameters);
 
-        CamelLogger camelLogger = new CamelLogger(remaining, level, endpoint.getMarker());
+        CamelLogger camelLogger = null; 
+        if (providedLogger == null) {
+            camelLogger = new CamelLogger(remaining, level, endpoint.getMarker());
+        } else {
+            camelLogger = new CamelLogger(providedLogger, level, endpoint.getMarker());
+        }
         Processor logger;
         if (endpoint.getGroupSize() != null) {
             logger = new ThroughputLogger(camelLogger, endpoint.getGroupSize());
@@ -89,6 +96,17 @@ public class LogComponent extends UriEndpointComponent {
         return LoggingLevel.valueOf(levelText.toUpperCase(Locale.ENGLISH));
     }
 
+    /**
+     * Gets optional {@link Logger} instance from parameters. If non-null, the provided instance
will be used as
+     * {@link Logger} in {@link CamelLogger}
+     * 
+     * @param parameters
+     * @return
+     */
+    protected Logger getLogger(Map<String, Object> parameters) {
+        return getAndRemoveOrResolveReferenceParameter(parameters, "logger", Logger.class);
+    }
+
     public ExchangeFormatter getExchangeFormatter() {
         return exchangeFormatter;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/043449cb/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java
----------------------------------------------------------------------
diff --git a/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java
b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java
index 09e4ebc..e7faed4 100644
--- a/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java
+++ b/tests/camel-itest-osgi/src/test/java/org/apache/camel/itest/osgi/core/log/LogRouteTest.java
@@ -16,10 +16,21 @@
  */
 package org.apache.camel.itest.osgi.core.log;
 
+import java.io.File;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.impl.JndiRegistry;
 import org.apache.camel.itest.osgi.OSGiIntegrationTestSupport;
+import org.apache.karaf.tooling.exam.options.DoNotModifyLogOption;
+import org.apache.karaf.tooling.exam.options.KarafDistributionConfigurationFileReplacementOption;
 import org.junit.Test;
 import org.junit.runner.RunWith;
+import org.ops4j.pax.exam.Option;
+import org.ops4j.pax.exam.junit.Configuration;
 import org.ops4j.pax.exam.junit.JUnit4TestRunner;
+import org.slf4j.LoggerFactory;
+
+import static org.ops4j.pax.exam.OptionUtils.*;
 
 @RunWith(JUnit4TestRunner.class)
 public class LogRouteTest extends OSGiIntegrationTestSupport {
@@ -40,6 +51,14 @@ public class LogRouteTest extends OSGiIntegrationTestSupport {
     }
 
     @Test
+    public void testSendMessageToInfoLogWithSiftLogging() throws Exception {
+        template.sendBody("log:org.apache.camel.SIFT?level=info&logger=#mylogger", "<level>INFO</level>");
+        template.sendBody("log:org.apache.camel.SIFT?level=debug&logger=#mylogger", "<level>DEBUG</level>");
+        template.sendBody("log:org.apache.camel.SIFT?level=info&logger=#mylogger", "<level>INFO</level>");
+        template.sendBody("log:org.apache.camel.SIFT?level=error&logger=#mylogger", "<level>ERROR</level>");
+    }
+
+    @Test
     public void testSendMessageToBadLevel() throws Exception {
         try {
             template.sendBody("log:org.apache.camel.TEST?level=noSuchLevel", "<level>noSuchLevel</level>");
@@ -53,6 +72,33 @@ public class LogRouteTest extends OSGiIntegrationTestSupport {
     public boolean isUseRouteBuilder() {
         return false;
     }
-   
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+        camelContext.setApplicationContextClassLoader(getClass().getClassLoader());
+        camelContext.setUseMDCLogging(true);
+        return camelContext;
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry registry = super.createRegistry();
+        registry.bind("mylogger", LoggerFactory.getLogger("org.apache.camel.SIFT"));
+        return registry;
+    }
+
+    @Configuration
+    public static Option[] configure() throws Exception {
+        Option[] options = combine(
+            getDefaultCamelKarafOptions(),
+            new Option[] {
+                new DoNotModifyLogOption(),
+                new KarafDistributionConfigurationFileReplacementOption("etc/org.ops4j.pax.logging.cfg",
new File("src/test/resources/log4j.properties")),
+//                KarafDistributionOption.debugConfiguration("9999", true)
+            });
+        return options;
+    }
+
 
 }

http://git-wip-us.apache.org/repos/asf/camel/blob/043449cb/tests/camel-itest-osgi/src/test/resources/log4j.properties
----------------------------------------------------------------------
diff --git a/tests/camel-itest-osgi/src/test/resources/log4j.properties b/tests/camel-itest-osgi/src/test/resources/log4j.properties
index dbf1670..2b05265 100644
--- a/tests/camel-itest-osgi/src/test/resources/log4j.properties
+++ b/tests/camel-itest-osgi/src/test/resources/log4j.properties
@@ -29,6 +29,9 @@ log4j.logger.org.apache.activemq.spring=WARN
 # is very noisy at INFO level
 log4j.logger.org.ops4j.io=WARN
 
+# sift test
+log4j.logger.org.apache.camel.SIFT=DEBUG, sift
+
 # CONSOLE appender not used by default
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
@@ -40,3 +43,13 @@ log4j.appender.out.layout=org.apache.log4j.PatternLayout
 log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
 log4j.appender.out.file=target/camel-itest-osgi-test.log
 log4j.appender.out.append=true
+
+# Sift appender
+log4j.appender.sift=org.apache.log4j.sift.MDCSiftingAppender
+log4j.appender.sift.key=bundle.name
+log4j.appender.sift.default=karaf
+log4j.appender.sift.appender=org.apache.log4j.FileAppender
+log4j.appender.sift.appender.layout=org.apache.log4j.PatternLayout
+log4j.appender.sift.appender.layout.ConversionPattern=%d{ISO8601} | id=%X{camel.exchangeId}
| %X{bundle.name} | %-5.5p |  %-32.32C %4L | %m%n
+log4j.appender.sift.appender.file=${karaf.data}/log/$\\{bundle.name\\}.log
+log4j.appender.sift.appender.append=true


Mime
View raw message