This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • MDC logging

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

Available as of Camel 2.7

In From Camel 2.7 we migrated to use slf4j as the : Camel uses the SLF4J logging framework in Camel. This allows us Camel to support MDC logging.
See For more details information about about MDC logging in see the logback manual.

The log kit logging framework in use must support MDC, such as. The following frameworks support MDC:

See the log kit documentation logging framework's documentation for how to configure and it to use MDC.

Enabling MDC Logging in Camel

To enable MDC logging in Camel you can do it as followsusing Java:

Code Block
languagejava
CamelContext context = ...
context.setUseMDCLogging(true);
...

In XML you enable it using the useMDCLogging attribute as followsTo enable MDC logging using Spring XML set CamelContext's useMDCLogging attribute:

Code Block
xml
xml

<camelContext xmlns="http://camel.apache.org/schema/spring" useMDCLogging="true">
  ...
</camelContext>

MDC

...

Information

Camel provides the following context information available for MDC:

Before Camel 2.

...

10:

 

Div
classconfluenceTableSmall

Key

Description

exchangeId

The exchange id

messageId

Camel 2.9.1: The message id

correlationId

The correlation id of the exchange if it's correlated. For example a sub message from the Splitter EIP

transactionKey

The id of the transaction for transacted exchanges. Note the id is not unique, but its the id of the transaction template that marks the transaction boundary for the given transaction. Hence we decided to name the key transactionKey and not transactionID to point out this fact.

routeId

The id of the route, in which the exchange is currently being routed

breadcrumbId

Camel 2.8: An unique id used for tracking messages across transports.

camelContextId

Camel 2.8.3/2.9: the camel context id used for tracking the message from different camel context.

From Camel 2.10

...

:

 

Div
classconfluenceTableSmall

Key

Description

camel.exchangeId

The exchange id

camel.messageId

The message id

camel.correlationId

The correlation id of the exchange if it's correlated. For example a sub message from the Splitter EIP

camel.transactionKey

The id of the transaction for transacted exchanges. Note the id is not unique, but its the id of the transaction template that marks the transaction boundary for the given transaction. Hence we decided to name the key transactionKey and not transactionID to point out this fact.

camel.routeId

The id of the route, in which the exchange is currently being routed

camel.breadcrumbId

An unique id used for tracking messages across transports.

camel.contextId

The camel context id used for tracking the message from different camel context.

The keys are subject to change as we want to align and leverage MDC across other Apache products such as ActiveMQ, ServiceMix and Karaf.

Example

...

Using LOG4J

If you use log4j you can configure MDC in the the log4j.properties file as shown:

Code Block
languagetext
log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %-10.10X{camel.exchangeId} - %-10.10X{camel.routeId} - %m%n

Camel will log on startup if MDC is enabled or not

Code Block
languagetext
INFO  SpringCamelContext -            -            - MDC logging is enabled on CamelContext: camel-1

The follow snippet is from an unit test which shows MDC in use. Notice Note that the exchange id and route id is are displayed in their respective separate columns in the log file:

Code Block
languagetext
INFO  SpringCamelContext -            -            - Apache Camel  (CamelContext: camel-1) started in 1.228 seconds
INFO  foo                - 358739-0-2 - route-a    - Exchange[ExchangePattern:InOnly, BodyType:String, Body:Hello World]
INFO  bar                - 358739-0-2 - route-b    - Exchange[ExchangePattern:InOnly, BodyType:String, Body:Hello World]
INFO  MockEndpoint       -            -            - Asserting: Endpoint[mock://result] is satisfied

Using breadcrumb

...

Enabling Breadcrumb Support

From Camel 2.8:
The breadcrumbId key for MDC logging is only available if useBreadcrumb option =true has been enabled set on the CamelContext (its default enabledis true). When enabled Camel will enrich the Camel Message by adding a header to it with the key breadcrumbId containing the id. Camel will use the the messageId if no existing existing breadcrumbId was found in the message. By storing

As the breadcrumb as id is stored in a header allow us to transport the breadcrumb across transports which supports message body and headers. For example HTTP and JMS and many other transports does that.

If you want to disable breadcrumb you do as follows in Java

...

it will persist across any transport that supports the use of headers, for example the HTTP and JMS transports.

Disabling Breadcrumb Support

Java DSL:

Code Block
languagejava
CamelContext context = ...
context.setUseBreadcrumb(false);
...

And in XML DSL:

Code Block
xml
xml

<camelContext xmlns="http://camel.apache.org/schema/spring" useBreadcrumb="false">
  ...
</camelContext>