Author: davsclaus
Date: Mon Aug 1 14:14:06 2011
New Revision: 1152789
URL: http://svn.apache.org/viewvc?rev=1152789&view=rev
Log:
CAMEL-4288: Added showFiles option to log formatter.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogFormatter.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/component/log/LogFormatter.java
URL:
http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogFormatter.java?rev=1152789&r1=1152788&r2=1152789&view=diff
==============================================================================
---
camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogFormatter.java
(original)
+++
camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogFormatter.java
Mon Aug 1 14:14:06 2011
@@ -47,6 +47,7 @@ public class LogFormatter implements Exc
private boolean multiline;
private boolean showFuture;
private boolean showStreams;
+ private boolean showFiles;
private int maxChars;
public String format(Exchange exchange) {
@@ -307,9 +308,22 @@ public class LogFormatter implements Exc
this.showStreams = showStreams;
}
+ public boolean isShowFiles() {
+ return showFiles;
+ }
+
+ /**
+ * If enabled Camel will output files
+ * <p/>
+ * Is default disabled.
+ */
+ public void setShowFiles(boolean showFiles) {
+ this.showFiles = showFiles;
+ }
+
// Implementation methods
//-------------------------------------------------------------------------
- protected Object getBodyAsString(Message message) {
+ protected String getBodyAsString(Message message) {
if (message.getBody() instanceof Future) {
if (!isShowFuture()) {
// just use a to string of the future object
@@ -317,11 +331,10 @@ public class LogFormatter implements Exc
}
}
- // is the body a stream cache then we can log it
- return MessageHelper.extractBodyForLogging(message, "",
isShowStreams(), -1);
+ return MessageHelper.extractBodyForLogging(message, "",
isShowStreams(), isShowFiles(), -1);
}
- protected Object getBodyTypeAsString(Message message) {
+ protected String getBodyTypeAsString(Message message) {
String answer = ObjectHelper.classCanonicalName(message.getBody());
if (answer != null && answer.startsWith("java.lang.")) {
return answer.substring(10);
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=1152789&r1=1152788&r2=1152789&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
Mon Aug 1 14:14:06 2011
@@ -21,6 +21,7 @@ import java.io.InputStream;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;
+import java.util.AbstractList;
import java.util.Map;
import java.util.TreeMap;
import javax.xml.transform.stream.StreamSource;
@@ -169,7 +170,8 @@ public final class MessageHelper {
maxChars =
message.getExchange().getContext().getTypeConverter().convertTo(Integer.class,
property);
}
}
- return extractBodyForLogging(message, prepend, streams, maxChars);
+
+ return extractBodyForLogging(message, prepend, streams, false,
maxChars);
}
/**
@@ -181,10 +183,11 @@ public final class MessageHelper {
* @param message the message
* @param prepend a message to prepend
* @param allowStreams whether or not streams is allowed
+ * @param allowFiles whether or not files is allowed
* @param maxChars limit to maximum number of chars. Use 0 or negative
value to not limit at all.
* @return the logging message
*/
- public static String extractBodyForLogging(Message message, String
prepend, boolean allowStreams, int maxChars) {
+ public static String extractBodyForLogging(Message message, String
prepend, boolean allowStreams, boolean allowFiles, int maxChars) {
Object obj = message.getBody();
if (obj == null) {
return prepend + "[Body is null]";
@@ -204,7 +207,7 @@ public final class MessageHelper {
return prepend + "[Body is instance of java.io.Reader]";
} else if (!allowStreams && obj instanceof Writer) {
return prepend + "[Body is instance of java.io.Writer]";
- } else if (obj instanceof GenericFile || obj instanceof File) {
+ } else if (!allowFiles && (obj instanceof GenericFile || obj
instanceof File)) {
return prepend + "[Body is file based: " + obj + "]";
}
@@ -287,7 +290,8 @@ public final class MessageHelper {
sb.append(">");
// dump body value as XML, use Camel type converter to convert to
String
- String xml = message.getBody(String.class);
+ // do not allow streams, but allow files, and clip very big message
bodies (128kb)
+ String xml = extractBodyForLogging(message, "", false, true, 128 *
1024);
if (xml != null) {
// must always xml encode
sb.append(StringHelper.xmlEncode(xml));