CAMEL-8091 DefaultExchangeFormatter should consider Exchange.LOG_DEBUG_BODY_MAX_CHARS with thanks to Stephan
Project: http://git-wip-us.apache.org/repos/asf/camel/repo Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0a252ecd Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0a252ecd Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0a252ecd Branch: refs/heads/camel-2.14.x Commit: 0a252ecd7ea5fe29e19595351fb52dcaf56c626f Parents: fedd88d Author: Willem Jiang <willem.ji...@gmail.com> Authored: Tue Dec 9 14:15:58 2014 +0800 Committer: Willem Jiang <willem.ji...@gmail.com> Committed: Tue Dec 9 14:23:49 2014 +0800 ---------------------------------------------------------------------- .../processor/DefaultExchangeFormatter.java | 13 ++- .../processor/DefaultExchangeFormatterTest.java | 91 ++++++++++++++++++++ 2 files changed, 103 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/camel/blob/0a252ecd/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 19ee87a..138c6cf 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 @@ -389,7 +389,18 @@ public class DefaultExchangeFormatter implements ExchangeFormatter { } } - return MessageHelper.extractBodyForLogging(message, "", isShowStreams(), isShowFiles(), getMaxChars()); + return MessageHelper.extractBodyForLogging(message, "", isShowStreams(), isShowFiles(), getMaxChars(message)); + } + + private int getMaxChars(Message message) { + int maxChars = getMaxChars(); + if (message.getExchange() != null) { + String property = message.getExchange().getContext().getProperty(Exchange.LOG_DEBUG_BODY_MAX_CHARS); + if (property != null) { + maxChars = message.getExchange().getContext().getTypeConverter().convertTo(Integer.class, property); + } + } + return maxChars; } protected String getBodyTypeAsString(Message message) { http://git-wip-us.apache.org/repos/asf/camel/blob/0a252ecd/camel-core/src/test/java/org/apache/camel/processor/DefaultExchangeFormatterTest.java ---------------------------------------------------------------------- diff --git a/camel-core/src/test/java/org/apache/camel/processor/DefaultExchangeFormatterTest.java b/camel-core/src/test/java/org/apache/camel/processor/DefaultExchangeFormatterTest.java new file mode 100644 index 0000000..69ee665 --- /dev/null +++ b/camel-core/src/test/java/org/apache/camel/processor/DefaultExchangeFormatterTest.java @@ -0,0 +1,91 @@ +/** + * 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.processor; + +import org.apache.camel.Exchange; +import org.apache.camel.Message; +import org.apache.camel.impl.DefaultCamelContext; +import org.apache.camel.impl.DefaultExchange; +import org.apache.camel.impl.DefaultMessage; +import org.junit.Before; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +public class DefaultExchangeFormatterTest { + private DefaultCamelContext camelContext; + private Exchange exchange; + private DefaultExchangeFormatter exchangeFormatter; + + @Before + public void setUp() { + camelContext = new DefaultCamelContext(); + Message message = new DefaultMessage(); + message.setBody("This is the message body"); + exchange = new DefaultExchange(camelContext); + exchange.setIn(message); + exchangeFormatter = new DefaultExchangeFormatter(); + } + + @Test + public void testDefaultFormat() { + String formattedExchange = exchangeFormatter.format(exchange); + assertTrue(formattedExchange.contains("This is the message body")); + } + + @Test + /* + * The formatted exchange without limitation is + * Exchange[ExchangePattern: InOnly, BodyType: String, Body: This is the message body] + * The "Exchange[", the "...", and the "]" do not count here, but the + * leading ", " that is removed later does count... + */ + public void testFormatWithMaxCharsParameter() { + exchangeFormatter.setMaxChars(60); + String formattedExchange = exchangeFormatter.format(exchange); + assertEquals(60 + "Exchange[...]".length() - ", ".length(), formattedExchange.length()); + } + + @Test + /* + * This limitation is really the length of the printed message body, not the + * one of the message + */ + public void testFormatWithBodyMaxChars() { + camelContext.getProperties().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "7"); + String formattedExchange = exchangeFormatter.format(exchange); + assertFalse(formattedExchange.contains("This is ")); + assertTrue(formattedExchange.contains("This is")); + camelContext.getProperties().remove(Exchange.LOG_DEBUG_BODY_MAX_CHARS); + } + + @Test + /* + * These two limitations will first truncate the message body and then the + * total message. + */ + public void testFormatWithBoth() { + camelContext.getProperties().put(Exchange.LOG_DEBUG_BODY_MAX_CHARS, "7"); + exchangeFormatter.setMaxChars(60); + String formattedExchange = exchangeFormatter.format(exchange); + assertEquals(60 + "Exchange[...]".length() - ", ".length(), formattedExchange.length()); + assertFalse(formattedExchange.contains("This is ")); + camelContext.getProperties().remove(Exchange.LOG_DEBUG_BODY_MAX_CHARS); + } +}