camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From conflue...@apache.org
Subject [CONF] Apache Camel > MDC logging
Date Sun, 04 Mar 2012 10:49:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2042/9/1/_/styles/combined.css?spaceKey=CAMEL&amp;forWysiwyg=true"
type="text/css">
    </head>
<body style="background: white;" bgcolor="white" class="email-body">
<div id="pageContent">
<div id="notificationFormat">
<div class="wiki-content">
<div class="email">
    <h2><a href="https://cwiki.apache.org/confluence/display/CAMEL/MDC+logging">MDC
logging</a></h2>
    <h4>Page <b>edited</b> by             <a href="https://cwiki.apache.org/confluence/display/~davsclaus">Claus
Ibsen</a>
    </h4>
        <br/>
                         <h4>Changes (3)</h4>
                                 
    
<div id="page-diffs">
                    <table class="diff" cellpadding="0" cellspacing="0">
    
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >Camel provides the following context
information available for MDC: <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Camel 2.9.x or older <br></td></tr>
            <tr><td class="diff-unchanged" >{div:class=confluenceTableSmall} <br>||
Key || Description || <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >{div} <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">h4.
Camel 2.10 onwards <br>{div:class=confluenceTableSmall} <br>|| Key || Description
|| <br>| {{camel.exchangeId}} | The exchange id | <br>| {{camel.messageId}} |
The message id | <br>| {{camel.correlationId}} | The correlation id of the exchange
if it&#39;s correlated. For example a sub message from the [Splitter] EIP | <br>|
{{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. | <br>| {{camel.routeId}} | The id of the route, in which the
exchange is currently being routed | <br>| {{camel.breadcrumbId}} | An unique id used
for tracking messages across transports. | <br>| {{camel.camelContextId}} | The camel
context id used for tracking the message from different camel context. | <br>{div} <br>
<br></td></tr>
            <tr><td class="diff-unchanged" >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/].  <br>
<br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >If you use log4j you can configure
MDC in the log4j.properties file as shown: <br>{code} <br></td></tr>
            <tr><td class="diff-changed-lines" >log4j.appender.out.layout.ConversionPattern=%d
[%-15.15t] %-5p %-30.30c{1} - <span class="diff-changed-words">%-10.10X{<span class="diff-added-chars"style="background-color:
#dfd;">cam</span>e<span class="diff-added-chars"style="background-color: #dfd;">l.e</span>xchangeId}</span>
- <span class="diff-changed-words">%-10.10X{<span class="diff-added-chars"style="background-color:
#dfd;">camel.</span>routeId}</span> - %m%n <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="MDClogging-MDClogging"></a>MDC logging</h2>
<p><b>Available as of Camel 2.7</b></p>

<p>In Camel 2.7 we migrated to use <a href="http://www.slf4j.org/" class="external-link"
rel="nofollow">slf4j</a> as the logging framework in Camel. This allows us to support
<a href="http://www.slf4j.org/api/org/slf4j/MDC.html" class="external-link" rel="nofollow">MDC
logging</a>.<br/>
See more details about MDC logging in the <a href="http://logback.qos.ch/manual/mdc.html"
class="external-link" rel="nofollow">logback manual</a>.</p>

<p>The log kit in use must support MDC, such as:</p>
<ul class="alternate" type="square">
	<li><a href="http://logging.apache.org/log4j/1.2/" class="external-link" rel="nofollow">log4j</a></li>
	<li><a href="http://logback.qos.ch/" class="external-link" rel="nofollow">logback</a></li>
	<li><a href="http://wiki.ops4j.org/display/paxlogging/Pax+Logging" class="external-link"
rel="nofollow">pax logging</a></li>
</ul>


<p>See the log kit documentation how to configure and use MDC.</p>

<h3><a name="MDClogging-EnablinginCamel"></a>Enabling in Camel</h3>

<p>To enable MDC logging in Camel you can do it as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CamelContext context = ...
context.setUseMDCLogging(<span class="code-keyword">true</span>);
...
</pre>
</div></div>

<p>In XML you enable it using the <tt>useMDCLogging</tt> attribute as follows:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
useMDCLogging=<span class="code-quote">"true"</span>&gt;</span>
  ...
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>

<h3><a name="MDClogging-MDCinformation"></a>MDC information</h3>

<p>Camel provides the following context information available for MDC:</p>

<h4><a name="MDClogging-Camel2.9.xorolder"></a>Camel 2.9.x or older</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Key </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>exchangeId</tt> </td>
<td class='confluenceTd'> The exchange id </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>messageId</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9.1:</b> The message id </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>correlationId</tt> </td>
<td class='confluenceTd'> The correlation id of the exchange if it's correlated. For
example a sub message from the <a href="/confluence/display/CAMEL/Splitter" title="Splitter">Splitter</a>
EIP </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>transactionKey</tt> </td>
<td class='confluenceTd'> 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 <tt>transactionKey</tt>
and not <tt>transactionID</tt> to point out this fact. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>routeId</tt> </td>
<td class='confluenceTd'> The id of the route, in which the exchange is currently being
routed </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>breadcrumbId</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> An unique id used for tracking
messages across transports. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camelContextId</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8.3/2.9:</b> the camel context id used
for tracking the message from different camel context. </td>
</tr>
</tbody></table>
</div>
</div>

<h4><a name="MDClogging-Camel2.10onwards"></a>Camel 2.10 onwards</h4>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Key </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel.exchangeId</tt> </td>
<td class='confluenceTd'> The exchange id </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel.messageId</tt> </td>
<td class='confluenceTd'> The message id </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel.correlationId</tt> </td>
<td class='confluenceTd'> The correlation id of the exchange if it's correlated. For
example a sub message from the <a href="/confluence/display/CAMEL/Splitter" title="Splitter">Splitter</a>
EIP </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel.transactionKey</tt> </td>
<td class='confluenceTd'> 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 <tt>transactionKey</tt>
and not <tt>transactionID</tt> to point out this fact. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel.routeId</tt> </td>
<td class='confluenceTd'> The id of the route, in which the exchange is currently being
routed </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel.breadcrumbId</tt> </td>
<td class='confluenceTd'> An unique id used for tracking messages across transports.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>camel.camelContextId</tt> </td>
<td class='confluenceTd'> The camel context id used for tracking the message from different
camel context. </td>
</tr>
</tbody></table>
</div>
</div>

<p>The keys are subject to change as we want to align and leverage MDC across other
Apache products such as <a href="http://activemq.apache.org" class="external-link" rel="nofollow">ActiveMQ</a>,
<a href="http://servicemix.apache.org/" class="external-link" rel="nofollow">ServiceMix</a>
and <a href="http://karaf.apache.org/" class="external-link" rel="nofollow">Karaf</a>.
</p>

<h3><a name="MDClogging-Exampleusinglog4j"></a>Example using log4j</h3>

<p>If you use log4j you can configure MDC in the log4j.properties file as shown:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %-10.10X{camel.exchangeId}
- %-10.10X{camel.routeId} - %m%n
</pre>
</div></div>

<p>Camel will log on startup if MDC is enabled or not</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
INFO  SpringCamelContext -            -            - MDC logging is enabled on CamelContext:
camel-1
</pre>
</div></div>

<p>The follow snippet is from an unit test which shows MDC in use. Notice the exchange
id and route id is displayed in their respective columns in the log file:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
INFO  SpringCamelContext -            -            - Apache Camel  (CamelContext: camel-1)
started in 1.228 seconds
INFO  foo                - 358739-0-2 - route-a    - Exchange[ExchangePattern:InOnly, BodyType:<span
class="code-object">String</span>, Body:Hello World]
INFO  bar                - 358739-0-2 - route-b    - Exchange[ExchangePattern:InOnly, BodyType:<span
class="code-object">String</span>, Body:Hello World]
INFO  MockEndpoint       -            -            - Asserting: Endpoint[mock:<span class="code-comment">//result]
is satisfied</span>
</pre>
</div></div>

<h3><a name="MDClogging-Usingbreadcrumb"></a>Using breadcrumb</h3>
<p><b>Available as of Camel 2.8</b><br/>
The <tt>breadcrumbId</tt> key for MDC logging is only available if <tt>useBreadcrumb</tt>
option has been enabled on <a href="/confluence/display/CAMEL/CamelContext" title="CamelContext">CamelContext</a>
(its default enabled). When enabled Camel will enrich the Camel <a href="/confluence/display/CAMEL/Message"
title="Message">Message</a> by adding a header to it with the key <tt>breadcrumbId</tt>
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 <a href="/confluence/display/CAMEL/HTTP"
title="HTTP">HTTP</a> and <a href="/confluence/display/CAMEL/JMS" title="JMS">JMS</a>
and many other transports does that.</p>

<p>If you want to disable breadcrumb you do as follows in Java</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-java">
CamelContext context = ...
context.setUseBreadcrumb(<span class="code-keyword">false</span>);
...
</pre>
</div></div>

<p>And in XML DSL</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="code-xml">
<span class="code-tag">&lt;camelContext xmlns=<span class="code-quote">"http://camel.apache.org/schema/spring"</span>
useBreadcrumb=<span class="code-quote">"false"</span>&gt;</span>
  ...
<span class="code-tag">&lt;/camelContext&gt;</span>
</pre>
</div></div>
    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;">
            <a href="https://cwiki.apache.org/confluence/users/viewnotifications.action"
class="grey">Change Notification Preferences</a>
        </div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/MDC+logging">View
Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=25200696&revisedVersion=11&originalVersion=10">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/MDC+logging?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message