You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2013/10/21 16:02:29 UTC

[2/2] git commit: CAMEL-6871: Added option to log component to control if message body should skip LS or not.

CAMEL-6871: Added option to log component to control if message body should skip LS or not.


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

Branch: refs/heads/camel-2.12.x
Commit: 063de3fc306ace354806c5234d5ee4c85063ee41
Parents: a72a3c2
Author: Claus Ibsen <da...@apache.org>
Authored: Mon Oct 21 15:51:54 2013 +0200
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon Oct 21 16:01:42 2013 +0200

----------------------------------------------------------------------
 .../processor/DefaultExchangeFormatter.java     | 46 +++++++----
 .../component/log/LogBodyWithNewLineTest.java   | 80 ++++++++++++++++++++
 2 files changed, 112 insertions(+), 14 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/063de3fc/camel-core/src/main/java/org/apache/camel/processor/DefaultExchangeFormatter.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/DefaultExchangeFormatter.java b/camel-core/src/main/java/org/apache/camel/processor/DefaultExchangeFormatter.java
index fe12d94..0eb6e80 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/DefaultExchangeFormatter.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/DefaultExchangeFormatter.java
@@ -34,6 +34,7 @@ import org.apache.camel.util.ObjectHelper;
 public class DefaultExchangeFormatter implements ExchangeFormatter {
 
     protected static final String LS = System.getProperty("line.separator");
+    private static final String SEPARATOR = "###REPLACE_ME###";
 
     public enum OutputStyle { Default, Tab, Fixed }
 
@@ -41,6 +42,7 @@ public class DefaultExchangeFormatter implements ExchangeFormatter {
     private boolean showExchangePattern = true;
     private boolean showProperties;
     private boolean showHeaders;
+    private boolean skipBodyLineSeparator = true;
     private boolean showBodyType = true;
     private boolean showBody = true;
     private boolean showOut;
@@ -72,40 +74,44 @@ public class DefaultExchangeFormatter implements ExchangeFormatter {
         StringBuilder sb = new StringBuilder();
         if (showAll || showExchangeId) {
             if (multiline) {
-                sb.append(LS);
+                sb.append(SEPARATOR);
             }
             sb.append(style("Id")).append(exchange.getExchangeId());
         }
         if (showAll || showExchangePattern) {
             if (multiline) {
-                sb.append(LS);
+                sb.append(SEPARATOR);
             }
             sb.append(style("ExchangePattern")).append(exchange.getPattern());
         }
 
         if (showAll || showProperties) {
             if (multiline) {
-                sb.append(LS);
+                sb.append(SEPARATOR);
             }
             sb.append(style("Properties")).append(sortMap(exchange.getProperties()));
         }
         if (showAll || showHeaders) {
             if (multiline) {
-                sb.append(LS);
+                sb.append(SEPARATOR);
             }
             sb.append(style("Headers")).append(sortMap(in.getHeaders()));
         }
         if (showAll || showBodyType) {
             if (multiline) {
-                sb.append(LS);
+                sb.append(SEPARATOR);
             }
             sb.append(style("BodyType")).append(getBodyTypeAsString(in));
         }
         if (showAll || showBody) {
             if (multiline) {
-                sb.append(LS);
+                sb.append(SEPARATOR);
             }
-            sb.append(style("Body")).append(getBodyAsString(in));
+            String body = getBodyAsString(in);
+            if (skipBodyLineSeparator) {
+                body = body.replaceAll(LS, "");
+            }
+            sb.append(style("Body")).append(body);
         }
 
         if (showAll || showException || showCaughtException) {
@@ -121,7 +127,7 @@ public class DefaultExchangeFormatter implements ExchangeFormatter {
 
             if (exception != null) {
                 if (multiline) {
-                    sb.append(LS);
+                    sb.append(SEPARATOR);
                 }
                 if (caught) {
                     sb.append(style("CaughtExceptionType")).append(exception.getClass().getCanonicalName());
@@ -143,25 +149,29 @@ public class DefaultExchangeFormatter implements ExchangeFormatter {
                 Message out = exchange.getOut();
                 if (showAll || showHeaders) {
                     if (multiline) {
-                        sb.append(LS);
+                        sb.append(SEPARATOR);
                     }
                     sb.append(style("OutHeaders")).append(sortMap(out.getHeaders()));
                 }
                 if (showAll || showBodyType) {
                     if (multiline) {
-                        sb.append(LS);
+                        sb.append(SEPARATOR);
                     }
                     sb.append(style("OutBodyType")).append(getBodyTypeAsString(out));
                 }
                 if (showAll || showBody) {
                     if (multiline) {
-                        sb.append(LS);
+                        sb.append(SEPARATOR);
+                    }
+                    String body = getBodyAsString(out);
+                    if (skipBodyLineSeparator) {
+                        body = body.replaceAll(LS, "");
                     }
-                    sb.append(style("OutBody")).append(getBodyAsString(out));
+                    sb.append(style("OutBody")).append(body);
                 }
             } else {
                 if (multiline) {
-                    sb.append(LS);
+                    sb.append(SEPARATOR);
                 }
                 sb.append(style("Out: null"));
             }
@@ -169,7 +179,7 @@ public class DefaultExchangeFormatter implements ExchangeFormatter {
 
         if (maxChars > 0) {
             StringBuilder answer = new StringBuilder();
-            for (String s : sb.toString().split(LS)) {
+            for (String s : sb.toString().split(SEPARATOR)) {
                 if (s != null) {
                     if (s.length() > maxChars) {
                         s = s.substring(0, maxChars);
@@ -227,6 +237,14 @@ public class DefaultExchangeFormatter implements ExchangeFormatter {
         this.showHeaders = showHeaders;
     }
 
+    public boolean isSkipBodyLineSeparator() {
+        return skipBodyLineSeparator;
+    }
+
+    public void setSkipBodyLineSeparator(boolean skipBodyLineSeparator) {
+        this.skipBodyLineSeparator = skipBodyLineSeparator;
+    }
+
     public boolean isShowBodyType() {
         return showBodyType;
     }

http://git-wip-us.apache.org/repos/asf/camel/blob/063de3fc/camel-core/src/test/java/org/apache/camel/component/log/LogBodyWithNewLineTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/component/log/LogBodyWithNewLineTest.java b/camel-core/src/test/java/org/apache/camel/component/log/LogBodyWithNewLineTest.java
new file mode 100644
index 0000000..0186571
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/component/log/LogBodyWithNewLineTest.java
@@ -0,0 +1,80 @@
+/**
+ * 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 java.io.StringWriter;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.log4j.Level;
+import org.apache.log4j.Logger;
+import org.apache.log4j.SimpleLayout;
+import org.apache.log4j.WriterAppender;
+
+public class LogBodyWithNewLineTest extends ContextTestSupport {
+
+    private StringWriter writer;
+
+    public void setUp() throws Exception {
+        super.setUp();
+        writer = new StringWriter();
+
+        WriterAppender appender = new WriterAppender(new SimpleLayout(), writer);
+        appender.setImmediateFlush(true);
+
+        Logger logger = Logger.getRootLogger();
+        logger.addAppender(appender);
+        logger.setLevel(Level.INFO);
+    }
+
+    public void testNoSkip() throws Exception {
+        final String ls = System.getProperty("line.separator");
+        String body = "1" + ls + "2" + ls + "3";
+
+        template.sendBody("direct:start", body);
+
+        log.info("{}", writer);
+
+        assertTrue(writer.toString().contains(body));
+    }
+
+    public void testSkip() throws Exception {
+        final String ls = System.getProperty("line.separator");
+        String body = "1" + ls + "2" + ls + "3";
+
+        template.sendBody("direct:skip", body);
+
+        log.info("{}", writer);
+
+        assertTrue(writer.toString().contains("123"));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .to("log:logger_name?level=INFO&showAll=true&skipBodyLineSeparator=false");
+
+                from("direct:skip")
+                    .to("log:logger_name?level=INFO&showAll=true&skipBodyLineSeparator=true");
+            }
+        };
+    }
+
+}