<?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>Andy Palmer &#187; log4j java stack-trace</title>
	<atom:link href="http://andypalmer.com/category/log4j-java-stack-trace/feed/" rel="self" type="application/rss+xml" />
	<link>http://andypalmer.com</link>
	<description>Views on software, technology, consulting and business process</description>
	<lastBuildDate>Sat, 14 Aug 2010 13:09:20 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Well, how did I get here?</title>
		<link>http://andypalmer.com/2008/08/well-how-did-i-get-here/</link>
		<comments>http://andypalmer.com/2008/08/well-how-did-i-get-here/#comments</comments>
		<pubDate>Mon, 25 Aug 2008 11:47:00 +0000</pubDate>
		<dc:creator>andy</dc:creator>
				<category><![CDATA[log4j java stack-trace]]></category>
		<category><![CDATA[advice]]></category>
		<category><![CDATA[development]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://www.andypalmer.com/blog/?p=11</guid>
		<description><![CDATA[And you may find yourself living in a shotgun shackAnd you may find yourself in another part of the worldAnd you may find yourself behind the wheel of a large automobileAnd you may find yourself in a beautiful house, with a beautiful wifeAnd you may ask yourself, &#8220;Well&#8230; how did I get here?&#8221;
Once in a [...]]]></description>
			<content:encoded><![CDATA[<p><quote><br />And you may find yourself living in a shotgun shack<br />And you may find yourself in another part of the world<br />And you may find yourself behind the wheel of a large automobile<br />And you may find yourself in a beautiful house, with a beautiful wife<br />And you may ask yourself, &#8220;Well&hellip; how did I get here?&#8221;</p>
<p><i>Once in a Lifetime &#8211; Talking Heads</i><br /></quote></p>
<h3>Logging a Stack Trace in log4j</h3>
<p>A while ago, I was trying to replace a number of hard coded strings with an appropriate Enum. Unfortunately, the design of the code I was working on meant that it was difficult to find all the possible values for the strings.<br />I decided that the behaviour that I wanted was:
<ul>
<li>If the string method was called with a new value, log that value, so that I can make a decision about whether I wish to add it to the Enum</li>
<p>
<li>If the string method was called with a value that matched a value in the Enum, I wanted a stack trace, so I could replace that call with the appropriate Enum call</li>
<p></ul>
<p>The code I ended up with was something like this:<br /><code class="prettyprint"><br />public void doSomething(String theDestination) {<br />  if(weKnowAbout(theDestination)) {<br />    logger.debug("doSomething called with a known destination of " + theDestination, new Throwable());<br />  }<br />  else {<br />    logger.debug("doSomething called with unknown destination of " + theDestination);<br />  }<br />  doWhatWeNeedToDoWith(theDestination);<br />}<br /></code></p>
<p>Passing the <code>new Throwable()</code> into log4j creates a stack trace in the log output. I&#8217;ve found a number of places that tell you how to do this (now that I know how to do it), but it took me quite a bit of searching when I was first looking.</p>
]]></content:encoded>
			<wfw:commentRss>http://andypalmer.com/2008/08/well-how-did-i-get-here/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
