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 Fri, 04 Feb 2011 09:07: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 (5)</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" >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} - %-10.10X{exchangeId} - <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">%-10.10X{correlationId}
-</span> %-10.10X{routeId} - %m%n <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <br> <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
            <tr><td class="diff-unchanged" >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: <br>{code} <br></td></tr>
            <tr><td class="diff-changed-lines" >INFO  SpringCamelContext -   
        -            - <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">
          -</span> Apache Camel  (CamelContext: camel-1) started in 1.228 seconds <br></td></tr>
            <tr><td class="diff-changed-lines" >INFO  foo                - 358739-0-2
- <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">
          -</span> route-a    - Exchange[ExchangePattern:InOnly, BodyType:String, Body:Hello
World] <br></td></tr>
            <tr><td class="diff-changed-lines" >INFO  bar                - 358739-0-2
- <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">
          -</span> route-b    - Exchange[ExchangePattern:InOnly, BodyType:String, Body:Hello
World] <br></td></tr>
            <tr><td class="diff-changed-lines" >INFO  MockEndpoint       -   
        -            - <span class="diff-deleted-words"style="color:#999;background-color:#fdd;text-decoration:line-through;">
          -</span> Asserting: Endpoint[mock://result] is satisfied <br></td></tr>
            <tr><td class="diff-unchanged" >{code} <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="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>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>
</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{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>
    </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=5&originalVersion=4">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