Camel supports a tracer interceptor that is used for logging the route executions at
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
Checkout which dependencies are required by Camel for logging purpose.
From Camel 2.12 onwards you must explicit enable tracing on CamelContext to use it. In XML DSL you do this by setting
<camelContext trace="true" ...> and in Java you do
Sets the Trace Formatter to use. Will default use
Flag to enable or disable this tracer
The logging level to use: FATAL, ERROR, WARN, INFO, DEBUG, TRACE, OFF
The log name to use. Will default use
An exchange Predicate to filter the tracing.
Flag to enable or disable tracing of interceptors
Flag to enable or disable tracing of thrown exception during processing of the exchange
Flag to enable fine grained tracing with a callback for both IN (before) and OUT (after). Is disabled by default which means there is only one trace callback executed.
When tracing exception you can control whether the stack trace should be logged also. If not then only the exception class and message is logged.
To use a
Camel 2.3: Fully class name for a custom
Camel 2.3: To use a custom
Camel 2.3: To use a custom
The tracer formats the execution of exchanges to log lines. They are logged at INFO level in the log category:
The tracer uses by default the
org.apache.camel.processor.interceptor.DefaultTraceFormatter to format the log line.
DefaultTraceFormatter has the following options:
Fixed length of the bread crumb. 0 = no fixed length. Setting a value to e.g. 80 allows the tracer logs to be aligned for easier reading.
Fixed length of the node. 0 = no fixed length. Setting a value to e.g. 40 allows the tracer logs to be aligned for easier reading.
Outputs the unique unit of work for the exchange. To be used for correlation so you can identify the same exchange.
Previous and destination node, so you can see from -> to.
To output the unique exchange id. Currently the breadcrumb is sufficient.
To output the unique exchange id in short form, without the hostname.
Output the exchange properties
Output the in message headers
Output the in body Java type
Output the in body
Output the out (if any) message headers
Output the out (if any) body Java type
Output the out (if any) body
Output the exchange pattern
Output the exception if the exchange has failed
Camel 2.8: Output the id of the route
Is used to limit the number of chars logged per line. The default value is
|multiline||false||Camel 2.18: If true, each piece of information is logged on a new line.|
Logging stream bodies
ID-claus-acer/3690-1214458315718/2-0 is the breadcrumb with the unique correlation id.
node3 is the id of the node in the route path. Is always shown.
To[mock:a] is the destination node.
InOnly is the exchange pattern. Is always shown.
Then the rest is properties, headers and the body.
Showing from and to
The trace log will output both the from and to so you can see where the Exchange came from, such as:
To enable tracer from the main run
and the tracer will be active.
Enabling from Java DSL
You can configure tracing at a higher granularity as you can configure it on camel context and then override and set it per route as well. For instance you could just enable tracer for one particular route.
Configuring from Java DSL
The tracer options can be configured from the Java DSL like this:
Using predicates to filter exchanges
In the code below we want the tracer only to trace if the body contains the text
London. As this is just an example can of course set any Predicate that matches your criteria:
Enabling from Spring XML
There is now a trace attribute you can specify on the *<camelContext/> for example
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 Spring
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.
Enable tracing of out messages
You can trace messages coming out of processing steps. To enable this, configure the tracer as followsor Running with these options, you'll get output similar to:
Using Custom Formatter
You can now implement your own
org.apache.camel.processor.interceptor.TraceFormatter to be used for logging trace messages to the log.
The sample below shows how to configure a Tracer from Java DSL using custom formatter:And here we have our custom logger that implements the
TraceFormatterinterface where we can construct the log message how we like:
Using Destination for custom processing and routing
Tracer supports custom processing of trace events. This can be used to route a trace event to a JPA endpoint for persistence in a database.
This works by Camel creates a new TraceEventMessage containing:
- snapshot of the original traced Exchange as a immutable TraceEventMessage containing String values of the fields, when the interception occurred. This ensures the fields contains the exact data at the given time of interception.
- the original Exchange can in some implementations be accessed using
getTracedExchange()(though with JPA based tracer you cannot get the original Exchange).
Beware to access the original Exchange to avoid causing any side effects or alter its state. Prefer to access the information from TraceEventMessage
Camel routes the TraceEventMessage synchronously from the point of interception. When its completed Camel will continue routing the original Exchange.
The sample below demonstrates this feature, where we route traced Exchanges to the
And our processor where we can process the TraceEventMessage. Here we want to create a CSV format of the trace event to be stored as a file. We do this by constructing the CSV String and the replace the IN body with our String instead of the
Using JPA as datastore for trace messages
See Tracer Example for complete documentation and how to use this feature.
Traced route path during runtime
The example below demonstrates how we can use that for error handling where we can determine at which node in the route graph the error triggered.
First we define our route: