...
MDC
...
logging
...
Available
...
as
...
of
...
Camel
...
2.7
...
From Camel 2.7: Camel uses the SLF4J logging framework. This allows Camel to support MDC logging. For more information about about MDC logging see the logback manual.
The logging framework in use must support MDC. The following frameworks support MDC:
See the logging framework's documentation for how to configure it to use MDC.
Enabling MDC Logging in Camel
To enable MDC logging using Java:
Code Block | ||
---|---|---|
| ||
we migrated to use [slf4j|http://www.slf4j.org/] as the logging framework in Camel. This allows us to support [MDC logging|http://www.slf4j.org/api/org/slf4j/MDC.html]. See more details about MDC logging in the [logback manual|http://logback.qos.ch/manual/mdc.html]. The log kit in use must support MDC, such as: - [log4j|http://logging.apache.org/log4j/1.2/] - [logback|http://logback.qos.ch/] - [pax logging|http://wiki.ops4j.org/display/paxlogging/Pax+Logging] See the log kit documentation how to configure and use MDC. h3. Enabling in Camel To enable MDC logging in Camel you can do it as follows: {code} CamelContext context = ... context.setUseMDCLogging(true); ... {code} In XML you enable it using the {{useMDCLogging}} attribute as follows: {code:xml} |
To enable MDC logging using Spring XML set CamelContext's useMDCLogging
attribute:
Code Block | ||||
---|---|---|---|---|
| ||||
<camelContext xmlns="http://camel.apache.org/schema/spring" useMDCLogging="true">
...
</camelContext>
{code}
h3. MDC information
Camel provides the following context information available for MDC:
{div:class=confluenceTableSmall}
|| Key || Description ||
| {{exchangeId}} | The exchange id |
| {{messageId}} | *Camel |
MDC Information
Camel provides the following context information available for MDC:
Before Camel 2.10:
Div | |||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| |||||||||||||||||||||||||||||||||||||||||
|
From Camel 2.10:
Div | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| ||||||||||||||||
|
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 log4j.properties
file as shown:
Code Block | ||
---|---|---|
| ||
| {div} The keys are subject to change as we want to align and leverage MDC across other Apache products such as [ActiveMQ|http://activemq.apache.org], [ServiceMix|http://servicemix.apache.org/] and [Karaf|http://karaf.apache.org/]. h3. Example using log4j If you use log4j you can configure MDC in the log4j.properties file as shown: {code} log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %-10.10X{camel.exchangeId} - %-10.10X{camel.routeId} - %m%n {code} |
Camel
...
will
...
log
...
on
...
startup
...
if
...
MDC
...
is
...
enabled
...
or
...
not
Code Block | ||
---|---|---|
| ||
{code} INFO SpringCamelContext - - - MDC logging is enabled on CamelContext: camel-1 {code} |
The
...
follow
...
snippet
...
is
...
from
...
an
...
unit
...
test
...
which
...
shows
...
MDC
...
in
...
use.
...
Note that the
...
exchange
...
id
...
and
...
route
...
id
...
are displayed
...
in
...
their
...
separate columns
...
in
...
the
...
log
...
file:
Code Block | ||
---|---|---|
| ||
{code} 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 {code} h3. Using breadcrumb *Available as of Camel |
Enabling Breadcrumb Support
From Camel 2.8:
The breadcrumbId
key for MDC logging is only available if useBreadcrumb=true
has been set on the CamelContext (default is 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 messageId
if no existing breadcrumbId
was found in the message.
As the breadcrumb id is stored in a header 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 | ||
---|---|---|
| ||
*
The {{breadcrumbId}} key for MDC logging is only available if {{useBreadcrumb}} option has been enabled on [CamelContext] (its default enabled). 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 messageId if no existing breadcrumbId was found in the message. By storing the breadcrumb as 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
{code}
CamelContext context = ...
context.setUseBreadcrumb(false);
...
|
XML DSL:
Code Block | ||||
---|---|---|---|---|
| ||||
{code} And in XML DSL {code:xml} <camelContext xmlns="http://camel.apache.org/schema/spring" useBreadcrumb="false"> ... </camelContext> {code} |