Tracer InterceptorCamel supports a tracer interceptor that is used for logging the route executions at INFO level. The Tracer is an InterceptStrategy which can be applied to a DefaultCamelContext or SpringCamelContext to ensure that there is a TracerInterceptor created for every node in the DSL. You can enable or disable the Tracer's logging dynamically, by calling the tracer's setEnabled method. OptionsTrace has been improved in Camel 1.5 to be more configurable with these options:
FormattingThe tracer formats the execution of exchanges to log lines. They are logged at INFO level in the log category: org.apache.camel.processor.interceptor.TraceInterceptor. TraceFormatter has the following options:
Example: ID-claus-acer/4412-1222625653890/2-0 -> to(mock:a) , Pattern:InOnly , Headers:{to=James} , BodyType:String , Body:Hello London
ID-claus-acer/3690-1214458315718/2-0 is the breadcrumb with the unique correlation id. EnablingTo enable tracer from the main run java org.apache.camel.spring.Main -t or java org.apache.camel.spring.Main -trace and the tracer will be active. Enabling from Java DSLThe tracer can be enabled by adding it to the interceptor chain to the camel context. This is demonstrated in the unit test below. public void testSendingSomeMessages() throws Exception { template.sendBodyAndHeader("direct:start", "Hello London", "to", "James"); template.sendBodyAndHeader("direct:start", "This is Copenhagen calling", "from", "Claus"); } protected RouteBuilder createRouteBuilder() throws Exception { return new RouteBuilder() { public void configure() throws Exception { // add tracer as an interceptor so it will log the exchange executions at runtime // this can aid us to understand/see how the exchanges is routed etc. getContext().addInterceptStrategy(new Tracer()); from("direct:start"). process(new Processor() { public void process(Exchange exchange) throws Exception { // do nothing } @Override public String toString() { return "MyProcessor"; } }). to("mock:a"). to("mock:b"); } }; } Running the test we get the trace information logged at INFO level: INFO TraceInterceptor - ID-claus-acer/4412-1222625653890/2-0 -> process(MyProcessor) , Pattern:InOnly , Headers:{to=James} , BodyType:String , Body:Hello London
INFO TraceInterceptor - ID-claus-acer/4412-1222625653890/2-0 -> to(mock:a) , Pattern:InOnly , Headers:{to=James} , BodyType:String , Body:Hello London
INFO TraceInterceptor - ID-claus-acer/4412-1222625653890/2-0 -> to(mock:b) , Pattern:InOnly , Headers:{to=James} , BodyType:String , Body:Hello London
INFO TraceInterceptor - ID-claus-acer/4412-1222625653890/2-1 -> process(MyProcessor) , Pattern:InOnly , Headers:{from=Claus} , BodyType:String , Body:This is Copenhagen calling
INFO TraceInterceptor - ID-claus-acer/4412-1222625653890/2-1 -> to(mock:a) , Pattern:InOnly , Headers:{from=Claus} , BodyType:String , Body:This is Copenhagen calling
INFO TraceInterceptor - ID-claus-acer/4412-1222625653890/2-1 -> to(mock:b) , Pattern:InOnly , Headers:{from=Claus} , BodyType:String , Body:This is Copenhagen calling
Configuring from Java DSLThe tracer options can be configured from the Java DSL like this: public void configure() throws Exception { // add tracer as an interceptor so it will log the exchange executions at runtime // this can aid us to understand/see how the exchanges is routed etc. Tracer tracer = new Tracer(); tracer.getFormatter().setShowBreadCrumb(false); tracer.getFormatter().setShowNode(false); ... getContext().addInterceptStrategy(tracer); Using predicates to filter exchangesAvailable as of Camel 1.5 Tracer tracer = new Tracer(); // set the level to FATAL so we can easily spot it tracer.setLogLevel(LoggingLevel.FATAL); // and only trace if the body contains London as text tracer.setTraceFilter(body().contains(constant("London"))); // do not show exchange pattern tracer.getFormatter().setShowExchangePattern(false); Enabling from Spring XMLThere is now a trace attribute you can specify on the *<camelContext/> for example <camelContext trace="true" xmlns="http://activemq.apache.org/camel/schema/spring"> ... </camelContext> You can see this in action with the SpringTraceTest Another option is to just include a spring XML which defines the Tracer bean such as the one that is automatically included if you run the Main with -t above Configuration from SpringIn Camel 1.5 you can configure the tracer as a Spring bean. Just add a bean with the bean class org.apache.camel.processor.interceptor.Tracer and Camel will use it as the Tracer. <!-- we can configure the tracer by defining the tracer bean and set the properties as we like --> <!-- the id name can be anything its the class that must be org.apache.camel.processor.interceptor.Tracer --> <bean id="camelTracer" class="org.apache.camel.processor.interceptor.Tracer"> <property name="traceExceptions" value="false"/> <property name="traceInterceptors" value="true"/> <property name="logLevel" value="FATAL"/> <property name="logName" value="com.mycompany.messages"/> </bean> <camelContext id="camel" trace="true" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="direct:start"/> <to uri="mock:result"/> </route> </camelContext> Formatting from SpringIn Camel 1.5 you can configure the formatting of tracer as a Spring bean. Just add a bean with the id traceFormatter and Camel will lookup this id and use the formatter, as the example below illustrates: <bean id="traceFormatter" class="org.apache.camel.processor.interceptor.TraceFormatter"> <property name="showBody" value="true"/> <property name="showBodyType" value="false"/> <property name="showBreadCrumb" value="false"/> </bean> <camelContext id="camel" trace="true" xmlns="http://activemq.apache.org/camel/schema/spring"> <route> <from uri="direct:start"/> <to uri="mock:result"/> </route> </camelContext> See Also |