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 2009/06/09 09:53:40 UTC

svn commit: r782900 - in /camel/trunk/camel-core/src: main/java/org/apache/camel/ main/java/org/apache/camel/impl/ main/java/org/apache/camel/util/ test/java/org/apache/camel/impl/

Author: davsclaus
Date: Tue Jun  9 07:53:39 2009
New Revision: 782900

URL: http://svn.apache.org/viewvc?rev=782900&view=rev
Log:
CAMEL-1579: Debug logging of messages now clips body if its too big. Added Camel property to set max length or disabled it by setting it to 0 or -1.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java?rev=782900&r1=782899&r2=782900&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/Exchange.java Tue Jun  9 07:53:39 2009
@@ -36,8 +36,8 @@
 
     String ASYNC_WAIT = "CamelAsyncWait";
 
-    String BATCH_INDEX = "CamelBatchIndex";
-    String BATCH_SIZE = "CamelBatchSize";
+    String BATCH_INDEX    = "CamelBatchIndex";
+    String BATCH_SIZE     = "CamelBatchSize";
     String BATCH_COMPLETE = "CamelBatchComplete";
 
     String BEAN_METHOD_NAME           = "CamelBeanMethodName";
@@ -69,8 +69,9 @@
 
     String INTERCEPTED_ENDPOINT = "CamelInterceptedEndpoint";
 
-    String LOOP_INDEX = "CamelLoopIndex";
-    String LOOP_SIZE  = "CamelLoopSize";
+    String LOG_DEBUG_BODY_MAX_CHARS = "CamelLogDebugBodyMaxChars";
+    String LOOP_INDEX               = "CamelLoopIndex";
+    String LOOP_SIZE                = "CamelLoopSize";
 
     String MULTICAST_INDEX = "CamelMulticastIndex";
 

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java?rev=782900&r1=782899&r2=782900&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/impl/DefaultMessage.java Tue Jun  9 07:53:39 2009
@@ -19,10 +19,10 @@
 import java.util.HashMap;
 import java.util.Map;
 import java.util.Set;
-
 import javax.activation.DataHandler;
 
 import org.apache.camel.Exchange;
+import org.apache.camel.util.MessageHelper;
 
 /**
  * The default implementation of {@link org.apache.camel.Message}
@@ -35,7 +35,7 @@
 
     @Override
     public String toString() {
-        return "Message: " + getBody();
+        return MessageHelper.extractBodyForLogging(this);
     }
 
     public Object getHeader(String name) {

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java?rev=782900&r1=782899&r2=782900&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/MessageHelper.java Tue Jun  9 07:53:39 2009
@@ -110,4 +110,35 @@
         }
         return contentType;
     }
+
+    /**
+     * Extracts the body for logging purpose.
+     * <p/>
+     * Will clip the body if its too big for logging.
+     *
+     * @see org.apache.camel.Exchange#LOG_DEBUG_BODY_MAX_CHARS
+     * @param message the message
+     * @return the logging message
+     */
+    public static String extractBodyForLogging(Message message) {
+        // default to 1000 chars
+        int length = 1000;
+
+        String property = message.getExchange().getContext().getProperties().get(Exchange.LOG_DEBUG_BODY_MAX_CHARS);
+        if (property != null) {
+            length = message.getExchange().getContext().getTypeConverter().convertTo(Integer.class, property);
+        }
+
+        String body = extractBodyAsString(message);
+        if (body == null) {
+            return "Message: [Body is null]";
+        }
+
+        // clip body if length enabled and the body is too big
+        if (length > 0 && body.length() > length) {
+            body = body.substring(0, length) + "... [Body clipped after " + length + " chars, total length is " + body.length() + "]";
+        }
+
+        return "Message: " + body;
+    }
 }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java?rev=782900&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java Tue Jun  9 07:53:39 2009
@@ -0,0 +1,84 @@
+/**
+ * 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.impl;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class LogDebugBodyMaxCharsTest extends ContextTestSupport {
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        context.getProperties().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "20");
+    }
+
+    public void testLogBodyMaxLengthTest() throws Exception {
+        // create a big body
+        StringBuffer sb = new StringBuffer();
+        for (int i = 0; i < 1000; i++) {
+            int value = i % 10;
+            sb.append(value);
+        }
+        String body = sb.toString();
+
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", body);
+
+        assertMockEndpointsSatisfied();
+
+        // should be clipped after 20 chars
+        String msg = mock.getReceivedExchanges().get(0).getIn().toString();
+        assertEquals("Message: 01234567890123456789... [Body clipped after 20 chars, total length is 1000]", msg);
+
+        // but body and clipped should not be the same
+        assertNotSame("clipped log and real body should not be the same", msg, mock.getReceivedExchanges().get(0).getIn().getBody(String.class));
+    }
+
+    public void tesNotClipped() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedMessageCount(1);
+
+        template.sendBody("direct:start", "1234567890");
+
+        assertMockEndpointsSatisfied();
+
+        // should be clipped after 20 chars
+        String msg = mock.getReceivedExchanges().get(0).getIn().toString();
+        assertEquals("Message: 01234567890", msg);
+
+        // but body and clipped should not be the same
+        assertNotSame("clipped log and real body should not be the same", msg, mock.getReceivedExchanges().get(0).getIn().getBody(String.class));
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start").to("log:foo").to("mock:result");
+            }
+        };
+    }
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/impl/LogDebugBodyMaxCharsTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date