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/07/16 16:27:25 UTC
svn commit: r794688 - in /camel/trunk/camel-core/src:
main/java/org/apache/camel/component/log/LogFormatter.java
test/java/org/apache/camel/component/log/LogFormatterTest.java
Author: davsclaus
Date: Thu Jul 16 14:27:24 2009
New Revision: 794688
URL: http://svn.apache.org/viewvc?rev=794688&view=rev
Log:
CAMEL-1836: log component can now output caught exception, for instance in a doCatch.
Modified:
camel/trunk/camel-core/src/main/java/org/apache/camel/component/log/LogFormatter.java
camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogFormatterTest.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=794688&r1=794687&r2=794688&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 Thu Jul 16 14:27:24 2009
@@ -37,6 +37,7 @@
private boolean showBody = true;
private boolean showOut;
private boolean showException;
+ private boolean showCaughtException;
private boolean showStackTrace;
private boolean showAll;
private boolean multiline;
@@ -76,16 +77,34 @@
}
sb.append(", Body:").append(getBodyAsString(in));
}
- if (exchange.getException() != null && (showAll || showException)) {
- if (multiline) {
- sb.append('\n');
+
+ if (showAll || showException || showCaughtException) {
+
+ // try exception on exchange first
+ Exception exception = exchange.getException();
+ boolean caught = false;
+ if (showCaughtException && exception == null) {
+ // fallback to caught exception
+ exception = exchange.getProperty(Exchange.EXCEPTION_CAUGHT, Exception.class);
+ caught = true;
}
- sb.append(", ExceptionType:").append(exchange.getException().getClass().getCanonicalName());
- sb.append(", ExceptionMessage:").append(exchange.getException().getMessage());
- if (showAll || showStackTrace) {
- StringWriter sw = new StringWriter();
- exchange.getException().printStackTrace(new PrintWriter(sw));
- sb.append(", StackTrace:").append(sw.toString());
+
+ if (exception != null) {
+ if (multiline) {
+ sb.append('\n');
+ }
+ if (caught) {
+ sb.append(", CaughtExceptionType:").append(exception.getClass().getCanonicalName());
+ sb.append(", CaughtExceptionMessage:").append(exception.getMessage());
+ } else {
+ sb.append(", ExceptionType:").append(exception.getClass().getCanonicalName());
+ sb.append(", ExceptionMessage:").append(exception.getMessage());
+ }
+ if (showAll || showStackTrace) {
+ StringWriter sw = new StringWriter();
+ exception.printStackTrace(new PrintWriter(sw));
+ sb.append(", StackTrace:").append(sw.toString());
+ }
}
}
@@ -214,6 +233,14 @@
this.showStackTrace = showStackTrace;
}
+ public boolean isShowCaughtException() {
+ return showCaughtException;
+ }
+
+ public void setShowCaughtException(boolean showCaughtException) {
+ this.showCaughtException = showCaughtException;
+ }
+
public boolean isMultiline() {
return multiline;
}
Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogFormatterTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogFormatterTest.java?rev=794688&r1=794687&r2=794688&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogFormatterTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/component/log/LogFormatterTest.java Thu Jul 16 14:27:24 2009
@@ -92,6 +92,18 @@
producer.stop();
}
+ public void testSendCaughtExchangeWithException() throws Exception {
+ Endpoint endpoint = resolveMandatoryEndpoint("log:org.apache.camel.TEST?showCaughtException=true");
+ Exchange exchange = endpoint.createExchange();
+ exchange.getIn().setBody("Hello World");
+ exchange.setProperty(Exchange.EXCEPTION_CAUGHT, new IllegalArgumentException("I am caught"));
+
+ Producer producer = endpoint.createProducer();
+ producer.start();
+ producer.process(exchange);
+ producer.stop();
+ }
+
public void testSendExchangeWithExceptionAndStackTrace() throws Exception {
Endpoint endpoint = resolveMandatoryEndpoint("log:org.apache.camel.TEST?showException=true&showStackTrace=true");
Exchange exchange = endpoint.createExchange();
@@ -104,4 +116,16 @@
producer.stop();
}
+ public void testSendCaughtExchangeWithExceptionAndStackTrace() throws Exception {
+ Endpoint endpoint = resolveMandatoryEndpoint("log:org.apache.camel.TEST?showCaughtException=true&showStackTrace=true");
+ Exchange exchange = endpoint.createExchange();
+ exchange.getIn().setBody("Hello World");
+ exchange.setProperty(Exchange.EXCEPTION_CAUGHT, new IllegalArgumentException("I am caught"));
+
+ Producer producer = endpoint.createProducer();
+ producer.start();
+ producer.process(exchange);
+ producer.stop();
+ }
+
}