<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Hitesh Sarda &#187; log4j</title>
	<atom:link href="http://hitesh.in/tag/log4j/feed/" rel="self" type="application/rss+xml" />
	<link>http://hitesh.in</link>
	<description>Thoughts on life, technology, education and entrepreneurship</description>
	<lastBuildDate>Fri, 30 Dec 2011 06:00:37 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Adding context to your logs</title>
		<link>http://hitesh.in/2010/adding-context-to-your-logs/</link>
		<comments>http://hitesh.in/2010/adding-context-to-your-logs/#comments</comments>
		<pubDate>Sat, 06 Mar 2010 23:05:58 +0000</pubDate>
		<dc:creator>Hitesh</dc:creator>
				<category><![CDATA[Technology]]></category>
		<category><![CDATA[log4j]]></category>
		<category><![CDATA[log4net]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://hitesh.in/?p=527</guid>
		<description><![CDATA[If you have ever worked on a multi-threaded application or even any application of significant scope, one of the unwritten requirement is a log file that helps in supporting the application. The challenge is to be able to piece together &#8230; <a href="http://hitesh.in/2010/adding-context-to-your-logs/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignright" style="width: 326px"><a href="http://www.flickr.com/photos/joysaphine/4366947275/"><img class=" " title="Logs with markings" src="http://farm5.static.flickr.com/4045/4366947275_0327144c7b.jpg" alt="" width="316" height="237" /></a><p class="wp-caption-text">joysaphine @ flickr</p></div>
<p>If you have ever worked on a multi-threaded application or even any application of significant scope, one of the unwritten requirement is a log file that helps in supporting the application.</p>
<p>The challenge is to be able to piece together all the scattered log entries that belong together, may be as a single transaction, or a user&#8217;s session. So the goal is to have a unique identifier in each log entry that will aid <em>greping.</em> If you have ever faced this, you either did it cleanly as I will detail below, or you had to hack in a &#8216;context&#8217; to each log entry by appending / prefixing some unique id.</p>
<p>I will talk about the two most commonly used logging frameworks are <a href="http://logging.apache.org/" target="_blank">Apache&#8217;s log4j and log4net</a>.</p>
<h4>The bad (obvious) way:</h4>
<pre>logger.<span style="color: #00aa00;"><strong>info</strong></span>(uid + " I am doing something important");
logger.<span style="color: #00aa00;"><strong>debug</strong></span>(uid + " I am doing something else");
</pre>
<p>The problems with this approach are</p>
<ul>
<li>might not have access to the uid in each function that is called</li>
<li>need to remember to append the uid everywhere</li>
<li>no way to do it in common code, utility libraries, etc.</li>
</ul>
<p>The good news is, support for such a use case is baked into log4j and log4net.<br />
<span id="more-527"></span></p>
<h3>Mapped Diagnostics Context &#8211; log4j <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/MDC.html" target="_blank">MDC</a></h3>
<p>MDC class has a thread local hashtable, in which you need to inject your context at the beginning of the transaction. All subsequent log entries will automatically contain the context as well.</p>
<pre>MDC.<span style="color: #00aa00;"><strong>put</strong></span>("uid", uid);
logger.<span style="color: #00aa00;"><strong>info</strong></span>("I am doing something important");
logger.<span style="color: #00aa00;"><strong>debug</strong></span>("I am doing something else");

logger.<span style="color: #00aa00;"><strong>debug</strong></span>("I am in a completely different code block.");
</pre>
<p>You can inject as many key/value pairs into MDC as you like. To print the values contained in the MDC context, change the logging format to include the %X{uid} or whatever keys you have passed to the MDC. Something like:</p>
<pre>log4j.appender.R.layout.ConversionPattern=%p %t %c <strong>%X{uid}</strong> - %m%n
</pre>
<p>Now each log entry will contain the uid that was set in the beginning of the transaction.</p>
<h3>ThreadContext.Properties &#8211; log4net</h3>
<p>The equivalent in log4net is the <a href="http://logging.apache.org/log4net/release/sdk/log4net.ThreadContext.html" target="_blank">ThreadContext.Properties</a></p>
<p>The usage is pretty similar, instead of MDC.put, call</p>
<pre>ThreadContext.Properties["uid"] = uid;</pre>
<p>and include <strong>%property{uid}</strong> in the output format. Something like:</p>
<pre id="pre1" lang="xml">&lt;conversionPattern value="%timestamp [%thread] %level %logger <strong>%property{uid}</strong> - %message%newline" /&gt;
</pre>
<p>That is all, ain&#8217;t this cleaner? BTW, have a look at <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html">NDC</a> / <a href="http://logging.apache.org/log4net/release/sdk/log4net.ThreadContext.Stacks.html">ThreadContext.Stacks</a> as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://hitesh.in/2010/adding-context-to-your-logs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

