<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<body link="#355491" alink="#4262a1" vlink="#355491" style="background: #e2e2e2; margin: 0; padding: 20px;">

<div>
        <table cellpadding="0" bgcolor="#FFFFFF" border="0" cellspacing="0" style="border: 1px solid #dadada; margin-bottom: 30px; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                <tbody>
                        <tr>

                                <td>

                                        <table border="0" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF" style="border: solid 2px #ccc; background: #dadada; width: 100%; -moz-border-radius: 6px; -webkit-border-radius: 6px;">
                                                <tbody>
                                                        <tr>
                                                                <td bgcolor="#000000" valign="middle" height="58px" style="border-bottom: 1px solid #ccc; padding: 20px; -moz-border-radius-topleft: 3px; -moz-border-radius-topright: 3px; -webkit-border-top-right-radius: 5px; -webkit-border-top-left-radius: 5px;">
                                                                        <h1 style="color: #333333; font: bold 22px Arial, Helvetica, sans-serif; margin: 0; display: block !important;">
                                                                        <!-- To have a header image/logo replace the name below with your img tag -->
                                                                        <!-- Email clients will render the images when the message is read so any image -->
                                                                        <!-- must be made available on a public server, so that all recipients can load the image. -->
                                                                        <a href="http://community.jboss.org/index.jspa" style="text-decoration: none; color: #E1E1E1">JBoss Community</a></h1>
                                                                </td>

                                                        </tr>
                                                        <tr>
                                                                <td bgcolor="#FFFFFF" style="font: normal 12px Arial, Helvetica, sans-serif; color:#333333; padding: 20px;  -moz-border-radius-bottomleft: 4px; -moz-border-radius-bottomright: 4px; -webkit-border-bottom-right-radius: 5px; -webkit-border-bottom-left-radius: 5px;"><h3 style="margin: 10px 0 5px; font-size: 17px; font-weight: normal;">
    Logging Contextual Information
</h3>
<span style="margin-bottom: 10px;">
    modified by <a href="http://community.jboss.org/people/admin">Administrator Administrator</a> in <i>EJB3</i> - <a href="http://community.jboss.org/docs/DOC-12847">View the full document</a>
</span>
<hr style="margin: 20px 0; border: none; background-color: #dadada; height: 1px;">

<div class="jive-rendered-content"><h4>Introduction</h4><p>Informative, granular log statements are essential to any server-side application.&#160; JBoss relies on the popular Log4J framework to bring this capability to users.&#160; However, typical logging statments may not always contain enough information to truly understand what's happening.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Almost everything that happens within the scope of a Java EE service invocation -- whether it be Servlet, EJB or anything else -- has interesting context that can be useful to someone reading the log file.&#160; We might like to know the JTA Transaction ID during an EJB invocation or the name of the current user during a Servlet request.&#160; This is quite simple using Log4J, but in a busy application, you might end up with hundreds of log lines every second and have no way to correlate one flow through the application from another.</p><h4>Contextual Logging with Log4J</h4><p>Logging something trivial from an EJB such as the number of rows returned by a query may be useful, but it would be far better if we knew what context it happened in.&#160; Most EJBs execute in within a JTA Transaction so why not add that to the log?&#160; It's relatively easy to log Transaction information at the beginning of a method:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">UserTransaction utx = (UserTransaction) <font color="navy"><b>new</b></font> InitialContext().lookup(<font color="red">"java:/UserTransaction"</font>);
log.debug(utx.toString());
</code></pre><p>But what about container managed transactions?&#160; What if we don't want to pollute our business code with endless logging details?</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Log4J includes a very useful feature called Mapped Diagnostic Contexts, or <a class="jive-link-external-small" href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html">MDC</a> for short.&#160; MDC gives us a way to add contextual information orthogonally to the code that's actually logging information.&#160; The MDC class contains static methods used to put and remove key/value pairs which can then be accessed by the Appender through named tokens.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>We can manage information in the MDC like such:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java">MDC.put(<font color="red">"name"</font>, <font color="red">"value"</font>);
...
log.debug(<font color="red">"Hello!"</font>);
...
MDC.remove(<font color="red">"name"</font>);
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In the above snippet, we have the option to output the value of the "name" information in the logs.&#160; To do this, we modify our conversion pattern using the %X token, specifying "name" as a parameter.&#160; By default, the Log4J configuration file resides in $JBOSS_HOME/server/&lt;config&gt;/conf/jboss-log4j.xml.</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag">&lt;layout class="org.apache.log4j.PatternLayout"&gt;</span>
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%X{name}] [%c{1}] %m%n"/&gt;</span>
<span class="jive-xml-tag">&lt;/layout&gt;</span>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>In this example, the [%X{name}] token will result in a log line similar to this:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">15:33:59,694 DEBUG&#160; [value] [MyClass] Hello!</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><h4>Logging Transaction ID for EJB3 Calls</h4><p>Now let's do something useful with this.&#160; I mentioned earlier that it would be interesting to log the JTA Transaction ID for EJB calls.&#160; I also showed that we can do this orthogonally to our business code so as not to pollute every method with nasty references to transaction APIs.&#160; What we need now is a place to add the Transaction ID to the MDC before a call begins and remove it after it returns.&#160; For this we'll use a standard EJB3 Interceptor.&#160; Here's an example implementation:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-java"><font color="navy"><b>public</b></font> <font color="navy"><b>class</b></font> LogContextInterceptor <font color="navy">{</font>
&#160;&#160;&#160; <font color="navy"><b>private</b></font> <font color="navy"><b>static</b></font> <font color="navy"><b>final</b></font> String TX_UID_KEY = <font color="red">"txid"</font>;
&#160;&#160;&#160; @Resource(mappedName=<font color="red">"java:/TransactionManager"</font>) <font color="navy"><b>private</b></font> TransactionManager tm;&#160;&#160;&#160; 
&#160;&#160;&#160; @AroundInvoke
&#160;&#160;&#160; <font color="navy"><b>public</b></font> Object intercept(InvocationContext ctx) <font color="navy"><b>throws</b></font> Exception <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; TransactionImple tx = (TransactionImple) tm.getTransaction();
&#160;&#160;&#160;&#160;&#160;&#160;&#160; MDC.put(TX_UID_KEY, tx.get_uid());
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>try</b></font> <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy"><b>return</b></font> ctx.proceed();
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">}</font> <font color="navy"><b>finally</b></font> <font color="navy">{</font>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; MDC.remove(TX_UID_KEY);
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <font color="navy">}</font>
&#160;&#160;&#160; <font color="navy">}</font>
<font color="navy">}</font>
</code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Note that this implementation uses the JBoss specific Arjuna TransactionImple so is isn't portable.&#160; This class can be compiled and shipped as part of your EJB Jar.&#160; By itself, however, it won't intercept anything!&#160; Next we need a deployment descriptor that specifies that this Interceptor be appled to every method.&#160; Here's an example ejb-jar.xml:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag">&lt;?xml version="1.0" encoding="UTF-8"?&gt;</span>
<span class="jive-xml-tag"><span>&lt;ejb-jar version="3.0" xmlns="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span>"
&#160;&#160;&#160; xmlns:xsi="</span><a class="jive-link-external-small" href="http://www.w3.org/2001/XMLSchema-instance" target="_blank">http://www.w3.org/2001/XMLSchema-instance</a><span>"
&#160;&#160;&#160; xsi:schemaLocation="</span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee" target="_blank">http://java.sun.com/xml/ns/javaee</a><span> </span><a class="jive-link-external-small" href="http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd" target="_blank">http://java.sun.com/xml/ns/javaee/ejb-jar_3_0.xsd</a><span>"&gt;</span></span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;assembly-descriptor&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;interceptor-binding&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;ejb-name&gt;</span>*<span class="jive-xml-tag">&lt;/ejb-name&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;interceptor-class&gt;</span>org.jboss.example.logging.LogContextInterceptor<span class="jive-xml-tag">&lt;/interceptor-class&gt;</span>
&#160;&#160;&#160;&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/interceptor-binding&gt;</span>
&#160;&#160;&#160; <span class="jive-xml-tag">&lt;/assembly-descriptor&gt;</span>
<span class="jive-xml-tag">&lt;/ejb-jar&gt;</span>
</code></pre><p>Don't forget to add the "txid" token to Log4J's ConversionPattern:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code jive-xml"><span class="jive-xml-tag">&lt;layout class="org.apache.log4j.PatternLayout"&gt;</span>
&#160;&#160;&#160;&#160; <span class="jive-xml-tag">&lt;param name="ConversionPattern" value="%d{ABSOLUTE} %-5p [%X{txid}] [%c{1}] %m%n"/&gt;</span>
<span class="jive-xml-tag">&lt;/layout&gt;</span></code></pre><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><p>Now let's look at the output from a basic SLSB invocation:</p><p style="min-height: 8pt; height: 8pt; padding: 0px;">&#160;</p><pre class="jive-pre"><code class="jive-code">16:39:10,184 INFO&#160; [7f000001:c958:48fe3ca7:124] [TransactionalServiceBean] Echoing string Hello World!
</code></pre><p><br/>That's it!&#160; Now you've got something to tie log lines to or use as a grep token.&#160; This general pattern can be used to output anything imaginable.</p></div>

<div style="background-color: #f4f4f4; padding: 10px; margin-top: 20px;">
    <p style="margin: 0;">Comment by <a href="http://community.jboss.org/docs/DOC-12847">going to Community</a></p>

        <p style="margin: 0;">Create a new document in EJB3 at <a href="http://community.jboss.org/choose-container!input.jspa?contentType=102&containerType=14&container=2029">Community</a></p>
</div></td>
                        </tr>
                    </tbody>
                </table>


                </td>
            </tr>
        </tbody>
    </table>

</div>

</body>
</html>