{"id":11,"date":"2008-08-25T11:47:00","date_gmt":"2008-08-25T11:47:00","guid":{"rendered":"http:\/\/www.andypalmer.com\/blog\/?p=11"},"modified":"2009-05-22T13:09:10","modified_gmt":"2009-05-22T13:09:10","slug":"well-how-did-i-get-here","status":"publish","type":"post","link":"https:\/\/andypalmer.com\/2008\/08\/well-how-did-i-get-here\/","title":{"rendered":"Well, how did I get here?"},"content":{"rendered":"<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, “Well… how did I get here?”<\/p>\n<p><i>Once in a Lifetime – Talking Heads<\/i><br \/><\/quote><\/p>\n<h3>Logging a Stack Trace in log4j<\/h3>\n<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:<\/p>\n<ul><\/p>\n<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>\n<p><\/p>\n<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>\n<p><\/ul>\n<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>\n<p>Passing the <code>new Throwable()<\/code> into log4j creates a stack trace in the log output. I’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>\n","protected":false},"excerpt":{"rendered":"<p>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, “Well… how did I get here?” Once in […]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[20,17,23],"_links":{"self":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/11"}],"collection":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/comments?post=11"}],"version-history":[{"count":1,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/11\/revisions"}],"predecessor-version":[{"id":121,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/posts\/11\/revisions\/121"}],"wp:attachment":[{"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/media?parent=11"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/categories?post=11"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/andypalmer.com\/wp-json\/wp\/v2\/tags?post=11"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}