camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Claus Ibsen (Confluence)" <conflue...@apache.org>
Subject [CONF] Apache Camel > Log
Date Sat, 31 Aug 2013 12:04:00 GMT
<html>
<head>
    <base href="https://cwiki.apache.org/confluence">
            <link rel="stylesheet" href="/confluence/s/en/2176/1/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/Log">Log</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" >NOTE: the {{ExchangeFormatter}} gets
applied to *all Log endpoints within that Camel Context*. If you need different ExchangeFormatters
for different endpoints, just instantiate the LogComponent as many times as needed, and use
the relevant bean name as the endpoint prefix. <br> <br></td></tr>
            <tr><td class="diff-added-lines" style="background-color: #dfd;">From
*Camel 2.11.2/2.12.1* onwards when using a custom log formatter, you can specify parameters
in the log uri, which gets configured on the custom log formatter. Though when you do that
you should define the &quot;logForamtter&quot; as prototype scoped so its not shared
if you have different parameters, eg: <br> <br>{code} <br>&lt;bean name=&quot;logFormatter&quot;
class=&quot;com.xyz.MyCustomExchangeFormatter&quot; scope=&quot;prototype&quot;/&gt;
<br>{code} <br> <br>And then we can have Camel routes using the log uri
with different options: <br>{code} <br>&lt;to uri=&quot;log:foo?param1=foo&amp;amp;param2=100&quot;/&gt;
<br>... <br>&lt;to uri=&quot;log:bar?param1=bar&amp;amp;param2=200&quot;/&gt;
<br>{code} <br> <br></td></tr>
            <tr><td class="diff-unchanged" >{include:Endpoint See Also} <br>*
[Tracer] <br></td></tr>
            <tr><td class="diff-snipped" >...<br></td></tr>
    
            </table>
    </div>                            <h4>Full Content</h4>
                    <div class="notificationGreySide">
        <h2><a name="Log-LogComponent"></a>Log Component</h2>

<p>The <b>log:</b> component logs message exchanges to the underlying logging
mechanism.</p>

<p>Camel uses <a href="http://www.slf4j.org/" class="external-link" rel="nofollow">sfl4j</a>
which allows you to configure logging via, among others:</p>
<ul>
	<li><a href="http://logging.apache.org/log4j/" 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://java.sun.com/j2se/1.4.2/docs/api/java/util/logging/package-summary.html"
class="external-link" rel="nofollow">JDK Util Logging logging</a></li>
</ul>


<h3><a name="Log-URIformat"></a>URI format</h3>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
log:loggingCategory[?options]
</pre>
</div></div>

<p>Where <b>loggingCategory</b> is the name of the logging category to use.
You can append query options to the URI in the following format, <tt>?option=value&amp;option=value&amp;...</tt></p>

<p>For example, a log endpoint typically specifies the logging level using the <tt>level</tt>
option, as follows:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
log:org.apache.camel.example?level=DEBUG
</pre>
</div></div>

<p>The default logger logs every exchange (<em>regular logging</em>). But
Camel also ships with the <tt>Throughput</tt> logger, which is used whenever the
<tt>groupSize</tt> option is specified.</p>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Also a log in the DSL</b><br
/>There is also a <tt>log</tt> directly in the DSL, but it has a different
purpose. Its meant for lightweight and human logs. See more details at <a href="/confluence/display/CAMEL/LogEIP"
title="LogEIP">LogEIP</a>.</td></tr></table></div>

<h3><a name="Log-Options"></a>Options</h3>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Type </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>level</tt> </td>
<td class='confluenceTd'> <tt>INFO</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> Logging level to use. Possible values: <tt>ERROR</tt>,
<tt>WARN</tt>, <tt>INFO</tt>, <tt>DEBUG</tt>, <tt>TRACE</tt>,
<tt>OFF</tt> </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>marker</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <tt>String</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> An optional <a href="http://www.slf4j.org/api/org/slf4j/Marker.html"
class="external-link" rel="nofollow">Marker</a> name to use. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>groupSize</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <tt>Integer</tt> </td>
<td class='confluenceTd'> An integer that specifies a group size for throughput logging.</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>groupInterval</tt> </td>
<td class='confluenceTd'> <tt>null</tt> </td>
<td class='confluenceTd'> <tt>Integer</tt> </td>
<td class='confluenceTd'> If specified will group message stats by this time interval
(in millis) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>groupDelay</tt> </td>
<td class='confluenceTd'> <tt>0</tt> </td>
<td class='confluenceTd'> <tt>Integer</tt> </td>
<td class='confluenceTd'> Set the initial delay for stats (in millis) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>groupActiveOnly</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> <tt>boolean</tt> </td>
<td class='confluenceTd'> If true, will hide stats when no new messages have been received
for a time interval, if false, show stats regardless of message traffic </td>
</tr>
</tbody></table>
</div>
</div>
<p><b>note</b>: groupDelay and groupActiveOnly are only applicable when
using groupInterval</p>



<h3><a name="Log-Formatting"></a>Formatting</h3>
<p>The log formats the execution of exchanges to log lines. <br/>
By default, the log uses <tt>LogFormatter</tt> to format the log output, where
<tt>LogFormatter</tt> has the following options:</p>
<div class="confluenceTableSmall"><div class='table-wrap'>
<table class='confluenceTable'><tbody>
<tr>
<th class='confluenceTh'> Option </th>
<th class='confluenceTh'> Default </th>
<th class='confluenceTh'> Description </th>
</tr>
<tr>
<td class='confluenceTd'> <tt>showAll</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Quick option for turning all options on. (multiline, maxChars
has to be manually set if to be used) </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showExchangeId</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Show the unique exchange ID. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showExchangePattern</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Shows the Message Exchange Pattern (or MEP for short). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showProperties</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Show the exchange properties. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showHeaders</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Show the In message headers. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showBodyType</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Show the In body Java type. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showBody</tt> </td>
<td class='confluenceTd'> <tt>true</tt> </td>
<td class='confluenceTd'> Show the In body. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showOut</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If the exchange has an Out message, show the Out message.
</td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showException</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If the exchange has an exception, show the exception message
(no stack trace). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showCaughtException</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If the exchange has a caught exception, show the exception
message (no stack trace). A caught exception is stored as a property on the exchange (using
the key <tt>Exchange.EXCEPTION_CAUGHT</tt>) and for instance a <tt>doCatch</tt>
can catch exceptions. See <a href="/confluence/display/CAMEL/Try+Catch+Finally" title="Try
Catch Finally">Try Catch Finally</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showStackTrace</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Show the stack trace, if an exchange has an exception. Only
effective if one of <tt>showAll</tt>, <tt>showException</tt> or <tt>showCaughtException</tt>
are enabled. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showFiles</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.9:</b> Whether Camel should show file
bodies or not (eg such as java.io.File). </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showFuture</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> Whether Camel should show <tt>java.util.concurrent.Future</tt>
bodies or not. If enabled Camel could potentially wait until the <tt>Future</tt>
task is done. Will by default not wait. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>showStreams</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> <b>Camel 2.8:</b> Whether Camel should show stream
bodies or not (eg such as java.io.InputStream). Beware if you enable this option then you
may not be able later to access the message body as the stream have already been read by this
logger. To remedy this you will have to use <a href="/confluence/display/CAMEL/Stream+caching"
title="Stream caching">Stream caching</a>. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>multiline</tt> </td>
<td class='confluenceTd'> <tt>false</tt> </td>
<td class='confluenceTd'> If <tt>true</tt>, each piece of information is
logged on a new line. </td>
</tr>
<tr>
<td class='confluenceTd'> <tt>maxChars</tt> </td>
<td class='confluenceTd'>&nbsp;</td>
<td class='confluenceTd'> Limits the number of characters logged per line. The default
value is <tt>10000</tt> from <b>Camel 2.9</b> onwards. </td>
</tr>
</tbody></table>
</div>
</div>

<div class='panelMacro'><table class='tipMacro'><colgroup><col width='24'><col></colgroup><tr><td
valign='top'><img src="/confluence/images/icons/emoticons/check.gif" width="16" height="16"
align="absmiddle" alt="" border="0"></td><td><b>Logging stream bodies</b><br
/>For older versions of Camel that do not support the showFiles or showStreams properties
above, you can set the following property instead on the <a href="/confluence/display/CAMEL/CamelContext"
title="CamelContext">CamelContext</a> to log both stream and file bodies:
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
camelContext.getProperties().put(Exchange.LOG_DEBUG_BODY_STREAMS, true);
</pre>
</div></div></td></tr></table></div>


<h3><a name="Log-Regularloggersample"></a>Regular logger sample</h3>
<p>In the route below we log the incoming orders at <tt>DEBUG</tt> level
before the order is processed:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("activemq:orders").to("log:com.mycompany.order?level=DEBUG").to("bean:processOrder");
</pre>
</div></div>

<p>Or using Spring XML to define the route:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: xml; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
  &lt;route&gt;
    &lt;from uri="activemq:orders"/&gt;
    &lt;to uri="log:com.mycompany.order?level=DEBUG"/&gt;
    &lt;to uri="bean:processOrder"/&gt;
  &lt;/route&gt; 
</pre>
</div></div>

<h3><a name="Log-Regularloggerwithformattersample"></a>Regular logger with
formatter sample</h3>
<p>In the route below we log the incoming orders at <tt>INFO</tt> level
before the order is processed.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("activemq:orders").
    to("log:com.mycompany.order?showAll=true&amp;multiline=true").to("bean:processOrder");
</pre>
</div></div>

<h3><a name="Log-ThroughputloggerwithgroupSizesample"></a>Throughput logger
with groupSize sample</h3>
<p>In the route below we log the throughput of the incoming orders at <tt>DEBUG</tt>
level grouped by 10 messages.</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("activemq:orders").
    to("log:com.mycompany.order?level=DEBUG&amp;groupSize=10").to("bean:processOrder");
</pre>
</div></div>

<h3><a name="Log-ThroughputloggerwithgroupIntervalsample"></a>Throughput
logger with groupInterval sample</h3>

<p>This route will result in message stats logged every 10s, with an initial 60s delay
and stats should be displayed even if there isn't any message traffic.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
from("activemq:orders").
to("log:com.mycompany.order?level=DEBUG&amp;groupInterval=10000&amp;groupDelay=60000&amp;groupActiveOnly=false").to("bean:processOrder");
</pre>
</div></div>

<p>The following will be logged:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
"Received: 1000 new messages, with total 2000 so far. Last group took: 10000 millis which
is: 100 messages per second. average: 100"
</pre>
</div></div>

<h3><a name="Log-Fullcustomizationoftheloggingoutput"></a>Full customization
of the logging output</h3>

<p><b>Available as of Camel 2.11</b></p>

<p>With the options outlined in the <a href="#Log-Formatting">Formatting</a>
section, you can control much of the output of the logger. However, log lines will always
follow this structure:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
Exchange[Id:ID-machine-local-50656-1234567901234-1-2, ExchangePattern:InOut, 
Properties:{CamelToEndpoint=log://org.apache.camel.component.log.TEST?showAll=true, 
CamelCreatedTimestamp=Thu Mar 28 00:00:00 WET 2013}, 
Headers:{breadcrumbId=ID-machine-local-50656-1234567901234-1-1}, BodyType:String, Body:Hello
World, Out: null]
</pre>
</div></div>

<p>This format is unsuitable in some cases, perhaps because you need to...</p>

<ul>
	<li>... filter the headers and properties that are printed, to strike a balance between
insight and verbosity.</li>
	<li>... adjust the log message to whatever you deem most readable.</li>
	<li>... tailor log messages for digestion by log mining systems, e.g. Splunk.</li>
	<li>... print specific body types differently.</li>
	<li>... etc.</li>
</ul>


<p>Whenever you require absolute customization, you can create a class that implements
the <a href="http://camel.apache.org/maven/current/camel-core/apidocs/org/apache/camel/spi/ExchangeFormatter.html"
class="external-link" rel="nofollow"><tt>ExchangeFormatter</tt></a> interface.
Within the <tt>format(Exchange)</tt> method you have access to the full Exchange,
so you can select and extract the precise information you need, format it in a custom manner
and return it. The return value will become the final log message.</p>

<p>You can have the Log component pick up your custom <tt>ExchangeFormatter</tt>
in either of two ways:</p>

<p><b>Explicitly instantiating the LogComponent in your Registry:</b></p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;bean name="log" class="org.apache.camel.component.log.LogComponent"&gt;
   &lt;property name="exchangeFormatter" ref="myCustomFormatter" /&gt;
&lt;/bean&gt;
</pre>
</div></div>

<p><b>Convention over configuration:</b></p>

<p>Simply by registering a bean with the name <tt>logFormatter</tt>; the
Log Component is intelligent enough to pick it up automatically.</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" /&gt;
</pre>
</div></div>

<p>NOTE: the <tt>ExchangeFormatter</tt> gets applied to <b>all Log
endpoints within that Camel Context</b>. If you need different ExchangeFormatters for
different endpoints, just instantiate the LogComponent as many times as needed, and use the
relevant bean name as the endpoint prefix.</p>

<p>From <b>Camel 2.11.2/2.12.1</b> onwards when using a custom log formatter,
you can specify parameters in the log uri, which gets configured on the custom log formatter.
Though when you do that you should define the "logForamtter" as prototype scoped so its not
shared if you have different parameters, eg:</p>

<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;bean name="logFormatter" class="com.xyz.MyCustomExchangeFormatter" scope="prototype"/&gt;
</pre>
</div></div>

<p>And then we can have Camel routes using the log uri with different options:</p>
<div class="code panel" style="border-width: 1px;"><div class="codeContent panelContent">
<pre class="theme: Default; brush: java; gutter: false" style="font-size:12px; font-family:
ConfluenceInstalledFont,monospace;">
&lt;to uri="log:foo?param1=foo&amp;amp;param2=100"/&gt;
...
&lt;to uri="log:bar?param1=bar&amp;amp;param2=200"/&gt;
</pre>
</div></div>

<h3><a name="Log-SeeAlso"></a>See Also</h3>
<ul>
	<li><a href="/confluence/display/CAMEL/Configuring+Camel" title="Configuring Camel">Configuring
Camel</a></li>
	<li><a href="/confluence/display/CAMEL/Component" title="Component">Component</a></li>
	<li><a href="/confluence/display/CAMEL/Endpoint" title="Endpoint">Endpoint</a></li>
	<li><a href="/confluence/display/CAMEL/Getting+Started" title="Getting Started">Getting
Started</a></li>
</ul>

<ul>
	<li><a href="/confluence/display/CAMEL/Tracer" title="Tracer">Tracer</a></li>
	<li><a href="/confluence/display/CAMEL/How+do+I+use+log4j" title="How do I use log4j">How
do I use log4j</a></li>
	<li><a href="/confluence/display/CAMEL/How+do+I+use+Java+1.4+logging" title="How
do I use Java 1.4 logging">How do I use Java 1.4 logging</a></li>
	<li><a href="/confluence/display/CAMEL/LogEIP" title="LogEIP">LogEIP</a>
for using <tt>log</tt> directly in the DSL for human logs.</li>
</ul>

    </div>
        <div id="commentsSection" class="wiki-content pageSection">
        <div style="float: right;" class="grey">
                        <a href="https://cwiki.apache.org/confluence/users/removespacenotification.action?spaceKey=CAMEL">Stop
watching space</a>
            <span style="padding: 0px 5px;">|</span>
                <a href="https://cwiki.apache.org/confluence/users/editmyemailsettings.action">Change
email notification preferences</a>
</div>
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Log">View Online</a>
        |
        <a href="https://cwiki.apache.org/confluence/pages/diffpagesbyversion.action?pageId=63040&revisedVersion=39&originalVersion=38">View
Changes</a>
                |
        <a href="https://cwiki.apache.org/confluence/display/CAMEL/Log?showComments=true&amp;showCommentArea=true#addcomment">Add
Comment</a>
            </div>
</div>
</div>
</div>
</div>
</body>
</html>

Mime
View raw message