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 Thu, 03 Feb 2011 15:47:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/2036/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 (1)</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" >| {{exchangeId}} | The exchange id
| <br>| {{correlationId}} | The correlation id of the exchange if it&#39;s correlated.
For example a sub message from the [Splitter] EIP | <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">|
{{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></td></tr>
            <tr><td class="diff-unchanged" >| {{routeId}} | The id of the route,
in which the exchange is currently being routed | <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>

<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>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>
</tbody></table>
</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{exchangeId}
- %-10.10X{correlationId} - %-10.10X{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">
2011-02-03 11:19:20,570 [main           ] 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">
2011-02-03 11:19:21,798 [main           ] INFO  SpringCamelContext             -         
  -            -            - Apache Camel  (CamelContext: camel-1) started in 1.228 seconds
2011-02-03 11:19:21,892 [main           ] INFO  foo                            - 358739-0-2
-            - route-a    - Exchange[ExchangePattern:InOnly, BodyType:<span class="code-object">String</span>,
Body:Hello World]
2011-02-03 11:19:21,894 [main           ] INFO  bar                            - 358739-0-2
-            - route-b    - Exchange[ExchangePattern:InOnly, BodyType:<span class="code-object">String</span>,
Body:Hello World]
2011-02-03 11:19:21,896 [main           ] INFO  MockEndpoint                   -         
  -            -            - Asserting: Endpoint[mock:<span class="code-comment">//result]
is satisfied</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=3&originalVersion=2">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