<?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>Jabbering Giraffe &#187; logging</title>
	<atom:link href="http://happygiraffe.net/blog/tag/logging/feed/" rel="self" type="application/rss+xml" />
	<link>http://happygiraffe.net/blog</link>
	<description></description>
	<lastBuildDate>Wed, 19 Oct 2011 10:40:06 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>ASL (Apple System Log)</title>
		<link>http://happygiraffe.net/blog/2009/10/20/asl-apple-system-log/</link>
		<comments>http://happygiraffe.net/blog/2009/10/20/asl-apple-system-log/#comments</comments>
		<pubDate>Tue, 20 Oct 2009 19:42:58 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[osx]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1632</guid>
		<description><![CDATA[I&#8217;ve just been debugging a problem with the pulse-agent on a mac. One of the big questions we had was: where the heck are the logs? The pulse-agent is managed by launchd. Apparently, this logs all stdout and stderr via &#8230; <a href="http://happygiraffe.net/blog/2009/10/20/asl-apple-system-log/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just been debugging a problem with the pulse-agent on a mac.  One of the big questions we had was: where the heck are the logs?  The pulse-agent is managed by <a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man8/launchd.8.html#//apple_ref/doc/man/8/launchd">launchd</a>.  Apparently, this <a href="http://developer.apple.com/mac/library/technotes/tn2005/tn2083.html#SECLOGGING">logs all stdout and stderr via ASL</a>.</p>
<p>But what&#8217;s ASL?  Apparently, it&#8217;s the Apple System Log.  There&#8217;s a nice summary on <a href="http://boredzo.org/blog/archives/2008-01-19/next-week-apple-system-logger">Domain of the Bored</a>.  He also gives the key hint: you can use <a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/syslog.1.html">syslog(1)</a> to read the binary ASL files.</p>
<p>I didn&#8217;t delve too deeply into the flags.  It appeared that just running <code>syslog</code> spat out all the information I required.  But, it comes out encoded like <code>cat -v</code>.  But you can pipe it through <a href="http://developer.apple.com/mac/library/documentation/Darwin/Reference/ManPages/man1/unvis.1.html">unvis(1)</a> to clean that up.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ syslog <span style="color: #000000; font-weight: bold;">|</span> unvis <span style="color: #000000; font-weight: bold;">|</span> <span style="color: #c20cb9; font-weight: bold;">less</span></pre></div></div>

<p>Normally, <code>Console.app</code> would take care of all this transparently.  But when you&#8217;re ssh&#8217;d into a mac, that&#8217;s not an option.  So it&#8217;s good to know about syslog(1).</p>
<p>Looking closer at the flags, you can say <code>syslog -E safe</code> in place of piping through unvis(1).</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2009/10/20/asl-apple-system-log/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Logging in Java</title>
		<link>http://happygiraffe.net/blog/2008/09/03/logging-in-java/</link>
		<comments>http://happygiraffe.net/blog/2008/09/03/logging-in-java/#comments</comments>
		<pubDate>Tue, 02 Sep 2008 23:18:26 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1359</guid>
		<description><![CDATA[Logging has long been a pet peeve of mine. I find it intensely irritating to arrive at a tomcat installation and see a catalina.out file hundreds of megs large because some fool of a developer thought that System.out.println() was a &#8230; <a href="http://happygiraffe.net/blog/2008/09/03/logging-in-java/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Logging has long been a pet peeve of mine.  I find it intensely irritating to arrive at a tomcat installation and see a catalina.out file hundreds of megs large because some fool of a developer thought that <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/lang/System.html#out">System.out.println()</a> was a good logging tool.  I recently gave a presentation at work about logging, and this is the contents that it was based on (mildly rewritten).</p>
<h3 id="why">Why use logging?</h3>
<p>Well, you could use <code>System.out.println()</code>.  But as alluded to above, this is a very coarse tool.  There&#8217;s no way to turn it off without altering the code.  With a logging system, you get much greater control, including the ability to control what gets logged and where it ends up.  And you get to control it at runtime without altering your code.</p>
<p>Generally, proper logging gives you better visibility into your project and its behaviour.</p>
<p>Perhaps most importantly you should do it in order to not piss off your sysadmins by generating a very large non-rotateable file.</p>
<h3 id="which">Which logging system to use?</h3>
<p>There are quite a few out there.</p>
<ul>
<li><a href="http://logging.apache.org/log4j/1.2/index.html">log4j</a></li>
<li><a href="http://java.sun.com/javase/6/docs/technotes/guides/logging/index.html">JDK logging</a></li>
</ul>
<p>In addition, there are &#8220;abstraction libraries,&#8221; which allow you to choose different logging systems to actually use.  For example:</p>
<ul>
<li><a href="http://commons.apache.org/logging/">commons-logging</a></li>
<li><a href="http://www.slf4j.org/">slf4j</a></li>
</ul>
<p>My advice is that unless you&#8217;re writing a library that you expect 3rd parties to use (over whom you have no control), then you should stick to log4j.  It&#8217;s extremely widely used and well known.  It works very well.  It&#8217;s also wonderfully configurable.</p>
<p>If you do have to write a library for 3rd parties, then commons-logging is the more popular choice.  Whilst it certainly has it&#8217;s <a href="http://www.qos.ch/logging/thinkAgain.jsp">flaws</a>, it does appear to work for me.  Caveat emptor.</p>
<h3 id="jdklogging">But what about JDK Logging?</h3>
<p>JDK Logging was patterned after log4j.  Only they made the configuration a lot less flexible.  The main flaw (as I see it) is that you can only configure it via a system property, which means it&#8217;s inherently global.  When you&#8217;re running several webapps in the same JVM (via tomcat), this is a fatal flaw.</p>
<p>Actually, I&#8217;ve since learned that tomcat works around this by providing it&#8217;s own replacement implementation of <a href="http://java.sun.com/javase/6/docs/api/java/util/logging/LogManager.html">LogManager</a>.  Regardless, the configuration is still very anaemic when compared with log4j.</p>
<h3 id="what">What should I log?</h3>
<ul>
<li>At <strong>DEBUG</strong>, log whatever the hell you like so you know you&#8217;ve been down a particular code path, or what a certain piece of data looks like.</li>
<li>At <strong>INFO</strong>, support should be able to trace through transactions and figure out what&#8217;s going on.</li>
<li>At <strong>WARN</strong>, note things that could constitute a problem.</li>
<li>At <strong>ERROR</strong>, note things are are a problem.</li>
<li>At <strong>FATAL</strong>, note why you cannot continue (&#8220;my CPU is on fire&#8221;).</li>
</ul>
<p>All messages above <strong>DEBUG</strong> must be readable by non-developers — plain English, please!</p>
<p>Remember to include as much <em>context</em> as possible.  Don&#8217;t just note that something failed.  Say what the conditions were that caused that failure.</p>
<h3 id="beginning">Beginning log4j</h3>
<p>First, create a very simple <code>log4j.properties</code> (you can use <code>log4j.xml</code> but it&#8217;s more verbose and the extra features it makes available are rarely needed).</p>
<pre>
  log4j.rootLogger=WARN, stdout
  log4j.appender.stdout=org.apache.log4j.ConsoleAppender
  log4j.appender.stdout.layout=org.apache.log4j.SimpleLayout
  log4j.logger.com.me=INFO
</pre>
<p><strong>NB</strong>: I set the default logging level to <strong>WARN</strong> and left our code logging at <strong>INFO</strong>.  This is in order to reduce junk output from 3rd party libraries like Spring and Hibernate.  Of course, you can always enable that if needed.</p>
<p>Place this somewhere it will end up at the root of your classpath.</p>
<ul>
<li>For maven, put it in <code>src/main/resources/log4j.properties</code>.</li>
<li>For an ant project, put it in <code>src/log4j.properties</code>.</li>
</ul>
<p>Now, create a logger and use it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">com.me</span><span style="color: #339933;">;</span>
  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Non-static allows use of getClass().</span>
    <span style="color: #000000; font-weight: bold;">private</span> Logger LOG <span style="color: #339933;">=</span> Logger.<span style="color: #006633;">getLogger</span><span style="color: #009900;">&#40;</span>getClass<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> logSomething<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      LOG.<span style="color: #006633;">info</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;something&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>That should end up on your console. Hurrah!</p>
<p><strong>NB</strong>: Always using a consistent field name for your logger makes it much easier to grep for log messages later on.  It&#8217;s a good candidate for an <a href="http://eclipse.dzone.com/news/effective-eclipse-custom-templ">Eclipse template</a>.</p>
<h3 id="debug">How do I enable DEBUG for a single class/package?</h3>
<p>Add this to <code>log4j.properties</code>.</p>
<pre>
 log4j.logger.org.springframework=DEBUG
</pre>
<p>Everything else will continue to be logged at the level of the rootLogger, but Spring stuff will come out more verbosely.</p>
<p>To understand how this works, you have to understand the idea of &#8220;logger hierarchies.&#8221;  If you have configured the rootLogger as <strong>WARN</strong> and the &#8220;com.me.proj2&#8243; logger as <code>INFO</code>, this is how the logger hierarchy could end up looking.</p>
<div style="text-align:center;"><img src="http://happygiraffe.net/blog/wp-content/uploads/2008/09/logger-hierarchies.png" alt="logger-hierarchies.png" border="0" width="428" height="463" /></div>
<h3 id="output">How do I customize the output format?</h3>
<p>Configure a different <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/Layout.html">Layout</a> in log4j.properties. e.g. <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html">PatternLayout</a>.</p>
<pre>
 log4j.appender.stdout=org.apache.log4j.ConsoleAppender
 log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
 log4j.appender.stdout.layout.conversionPattern=%d{ISO8601} %c{2} %p %m%n
</pre>
<p>That configures each line with the following fields:</p>
<ul>
<li>The date &#038; time in standard <a href="http://www.w3.org/TR/NOTE-datetime">ISO 8601</a> format.</li>
<li>The last two components of the classname which made the log message.</li>
<li>The priority level.</li>
<li>The message itself.</li>
<li>A newline.</li>
</ul>
<h3 id="config">How does log4j get configured?</h3>
<p>The configuration process is fairly simple.   The first time you call log4j, it looks on the classpath for <code>log4j.xml</code> and then <code>log4j.properties</code>.  That&#8217;s all there is to it, <em>unless you change it</em>.</p>
<p>There are several ways to impose your own order on things.</p>
<ul>
<li>You can specify a <code>log4j.configuration</code> system property pointing a file.  But as we discussed earlier, system properties are generally best avoided.</li>
<li>You can use the log4j API (<a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PropertyConfigurator.html">PropertyConfigurator</a> or <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/xml/DOMConfigurator.html">DOMConfigurator</a>) to find the config file in the location you want.  You have to execute this code before the rest of your app starts up.</li>
</ul>
<p>If it still doesn&#8217;t seem to be doing what you expect, you can (as a one-off) specify the <code>log4j.debug</code> system property on the command line.  That will show you where log4j is looking for its configuration on stderr.</p>
<h3 id="configtests">How do I configure logging in my tests?</h3>
<p>Generally you don&#8217;t want logging in tests.  They&#8217;re verbose enough already, and JUnit will let you know when something&#8217;s wrong.  You can enforce this by creating a second <code>log4j.properties</code> that lives in the classpath of your tests, which sets the rootLogger to WARN (and nothing else more detailed).</p>
<ul>
<li>For maven, put it in <code>src/test/resources/log4j.properties</code>.</li>
<li>For an ant project, put it in <code>test/log4j.properties</code>.</li>
</ul>
<h3 id="configwebapp">How should I configure log4j in a webapp?</h3>
<p>As early as possible. This lets you see debug output from the rest of your app starting up.</p>
<p>That is, in a special <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html">ServletContextListener</a>.  Trying to configure log4j from within a Spring applicationContext is far, far too late (this appears to be a <a href="http://happygiraffe.net/blog/2008/08/27/logging-in-cocoon-22/">common mistake</a>).</p>
<p>Spring has a special purpose <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/util/Log4jConfigListener.html">Log4jConfigListener</a> for this purpose. You can write your own, however.  Here&#8217;s an example of the <code>web.xml</code> configuration.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>log4jConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/log4j.properties<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.util.Log4jConfigListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<h3 id="configwebappprod">How should log4j be setup for development / production in a webapp?</h3>
<p>Don&#8217;t have a default <code>/log4j.properties</code> on the classpath. Instead, have the Log4jConfigListener set up in the web.xml, pointing at <code>/WEB-INF/log4j.properties</code> (as shown above). That file should contain your <em>development</em> logging setup (INFO level output to the console) as well as commented out examples of production logging setup (WARN level output to a rotated file).</p>
<p>Create a file <code>META-INF/context.xml</code> (for tomcat), which contains a commented out override for the log4jConfigLocation context-param. e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"> <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
   <span style="color: #808080; font-style: italic;">&lt;!--</span>
<span style="color: #808080; font-style: italic;">   &lt;parameter override=&quot;false&quot;</span>
<span style="color: #808080; font-style: italic;">              name=&quot;log4jConfigLocation&quot;</span>
<span style="color: #808080; font-style: italic;">              value=&quot;file:///etc/myapp/log4j.properties&quot;</span>
<span style="color: #808080; font-style: italic;">              /&gt;</span>
<span style="color: #808080; font-style: italic;">   --&gt;</span>
 <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>When packaged as a war, the two files are easily extracted and can be used to override the logging behaviour.</p>
<pre>
 % unzip -p ~/foo.war META-INF/context.xml > $sitecode/conf/Catalina/localhost/foo.xml
 % vi $CATALINA_BASE/conf/Catalina/localhost/foo.xml
 % unzip -p ~/foo.war WEB-INF/log4j.properties > /etc/myapp/log4j.properties
 % vi /etc/myapp/log4j.properties
</pre>
<p>This is probably the most flexible solution.  It means that you can deploy the same war file anywhere, and just have a small amount of one-time setup to get it going.</p>
<h3 id="rotation">How do I get my log files rotated?</h3>
<p>Log4j comes with a builtin <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/DailyRollingFileAppender.html">DailyRollingFileAppender</a> which will give you a new log file every day.  However, I&#8217;ve tended to prefer the <a href="http://minaret.biz/tips/datedFileAppender.html">DatedFileAppender</a> addon, which works in a very similar manner to <a href="http://www.cronolog.org/">cronolog</a>.  That is, each log file is named with the current date in it.</p>
<p>This is how it&#8217;s configured in <code>log4j.properties</code>.</p>
<pre>
log4j.rootLogger = WARN, logfile
log4j.appender.logfile=biz.minaret.log4j.DatedFileAppender
log4j.appender.logfile.Directory=${catalina.base}/logs
log4j.appender.logfile.Prefix=myapp.
log4j.appender.logfile.Suffix=.txt
log4j.appender.logfile.layout=org.apache.log4j.PatternLayout
log4j.appender.logfile.layout.ConversionPattern=%d{ISO8601} %p %c{2} - %m%n
</pre>
<h3 id="exceptions">What about exceptions?</h3>
<p>Lots of people tend to log that an exception occurred and then move on as if nothing happened.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #666666; font-style: italic;">// FAIL</span>
  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    someOperation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>SomeNastyException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    LOG.<span style="color: #006633;">error</span><span style="color: #009900;">&#40;</span>e.<span style="color: #006633;">getMessage</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>This is bad because it loses all the context associated with the exception, as well as failing to stop the course of the program.  As I <a href="http://happygiraffe.net/blog/2008/08/05/exceptional-eclipse-tip/">noted before</a>, you&#8217;re much better off rethrowing it for something higher up to deal with.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    someOperation<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>SomeNastyException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">throw</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">RuntimeException</span><span style="color: #009900;">&#40;</span>e<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>Whilst this doesn&#8217;t work in all cases, it&#8217;s a far better default stance to take.</p>
<p>You might also want to look at <a href="http://www.onjava.com/pub/a/onjava/2003/11/19/exceptions.html">Best Practises For Exception Handling</a>.</p>
<h3 id="uniqueids">How do I get a unique per-request ID in my logs?</h3>
<p>Tools like postfix and sendmail include the message-id in the log files.  This is invaluable for tracing a message over several lines of a log file.  I needed to do this in a webapp and found log4j&#8217;s <a href="http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/NDC.html">Nested Diagnostic Context</a>.  This is how I implemented it.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> AddIdListener <span style="color: #000000; font-weight: bold;">implements</span> ServletRequestListener <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">long</span> requestNum <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">synchronized</span> <span style="color: #000066; font-weight: bold;">long</span> getNextRequestNum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      <span style="color: #000000; font-weight: bold;">return</span> requestNum<span style="color: #339933;">++;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> requestInitialized<span style="color: #009900;">&#40;</span>ServletRequestEvent sre<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      NDC.<span style="color: #006633;">push</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span>getNextRequestNum<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> requestDestroyed<span style="color: #009900;">&#40;</span>ServletRequestEvent sre<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      NDC.<span style="color: #006633;">pop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

<p>With this configured in the <code>web.xml</code>, you need to use the PatternLayout to get it into the logs.</p>
<pre>
 log4j.appender.stdout.layout.conversionPattern=%d{ISO8601} %x %c{2} %p %m%n
</pre>
<p>Now, all requests with the same value in the %x field belong to the same request.</p>
<p>This may not be a perfect solution, but it works quite well for me.</p>
<h3 id="performancetip">A performance tip</h3>
<p>Sometimes, a call to a logger can be expensive.  Particularly in the middle of a tight loop.  For this, you can ask log4j if it&#8217;s even going to bother using the output, which is quicker.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">  <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>BigHairyObject bho <span style="color: #339933;">:</span> bigHairyObjects<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>LOG.<span style="color: #006633;">isDebugEnabled</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
      LOG.<span style="color: #006633;">debug</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;here, bho=&quot;</span> <span style="color: #339933;">+</span> bho<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
  <span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/09/03/logging-in-java/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Logging in Cocoon 2.2</title>
		<link>http://happygiraffe.net/blog/2008/08/27/logging-in-cocoon-22/</link>
		<comments>http://happygiraffe.net/blog/2008/08/27/logging-in-cocoon-22/#comments</comments>
		<pubDate>Wed, 27 Aug 2008 00:41:27 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cocoon]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[logging]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/blog/?p=1331</guid>
		<description><![CDATA[I&#8217;ve had to try and understand logging in Cocoon 2.2 for a project at work recently. It&#8217;s been &#8220;interesting,&#8221; so I thought I&#8217;d blog the process in case anybody else needs to o this… Normally, logging in Java is quite &#8230; <a href="http://happygiraffe.net/blog/2008/08/27/logging-in-cocoon-22/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve had to try and understand logging in Cocoon 2.2 for a project at work recently.  It&#8217;s been &#8220;interesting,&#8221; so I thought I&#8217;d blog the process in case anybody else needs to o this…</p>
<p>Normally, logging in Java is quite simple: you add <a href="http://logging.apache.org/log4j/1.2/">log4j</a> to your classpath, then create a log4j.properties to say what gets logged.  If you&#8217;re running as part of a webapp, you use something like Spring&#8217;s <a href="http://static.springframework.org/spring/docs/2.5.x/api/org/springframework/web/util/Log4jConfigListener.html">Log4jConfigListener</a> to ensure that the configuration gets applied as soon as the webapp is active.</p>
<p>Cocoon is different.  By default (in development) you run it using a combination of the <a href="http://cocoon.apache.org/2.2/maven-plugins/maven-plugin/1.0/1359_1_1.html">cocoon-maven-plugin</a> and <code>mvn jetty:run</code>.  This is quite cunning (as <a href="http://reflectingonthevicissitudes.wordpress.com/">Grzegorz</a> explained in a <a href="http://happygiraffe.net/blog/2008/01/21/a-year-in-cocoon/#comment-542">comment</a> a while back), it lets you edit all sorts of stuff and have it dynamically reloaded.  In order to make the cocoon &#8220;block&#8221; work with jetty, the maven plugin creates things like web.xml for you automatically, so there&#8217;s no chance to edit things.  Drat.</p>
<p>Now, if you follow the documentation for <a href="http://cocoon.apache.org/2.2/core-modules/core/2.2/1257_1_1.html">logging in Cocoon</a>, it advises:</p>
<blockquote>
<p>The usual Cocoon web application sets up Log4j through the <a href="http://cocoon.apache.org/subprojects/configuration/1.0/spring-configurator/1.0/">Cocoon Spring Configurator</a>.</p>
</blockquote>
<p>Lovely advice, except that by the time Spring has started up, read your logging configuration and applied it, a great deal of interesting events have already occurred.  You really need to enable logging as early as possible using a <a href="http://java.sun.com/javaee/5/docs/api/javax/servlet/ServletContextListener.html">ServletContextListener</a>.</p>
<p>Thankfully, it&#8217;s possible to do so, even when using <code>mvn jetty:run</code>.</p>
<p>First, you need to create a log4j configuration that&#8217;s suitable.  I think it has to be XML, which is a shame as it&#8217;s more complicated than the properties file.  I wanted to change the defaults to get my FlowScript calls to log to the console.  This is what I ended up with in <code>etc/log4j.xml</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #00bbdd;">&lt;!DOCTYPE log4j:configuration SYSTEM &quot;log4j.dtd&quot;&gt;</span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;log4j:configuration</span> <span style="color: #000066;">xmlns:log4j</span>=<span style="color: #ff0000;">&quot;http://jakarta.apache.org/log4j/&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;stdout&quot;</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.log4j.ConsoleAppender&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;target&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;System.err&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;layout</span> <span style="color: #000066;">class</span>=<span style="color: #ff0000;">&quot;org.apache.log4j.PatternLayout&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
        <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;ConversionPattern&quot;</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;%d{ISO8601} %c{2} %p - %m%n&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/layout<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/appender<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;logger</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;cocoon&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;INFO&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/logger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;logger</span> <span style="color: #000066;">name</span>=<span style="color: #ff0000;">&quot;org.apache.cocoon.components.flow.javascript.fom&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;level</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;INFO&quot;</span> <span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/logger<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;priority</span> <span style="color: #000066;">value</span>=<span style="color: #ff0000;">&quot;WARN&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;appender-ref</span> <span style="color: #000066;">ref</span>=<span style="color: #ff0000;">&quot;stdout&quot;</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/root<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/log4j:configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Note that we shut everything up (WARN) by default, and then explicitly enable messages for things we want to see (INFO).  I&#8217;ve found that even in development, this helps to tell the wood from the trees.</p>
<p>With that in place, you have to edit <code>pom.xml</code> in order to tell the cocoon-maven-plugin to use that instead if its default.  The default pom should have a build/plugins/plugin section, to which this stanza needs adding.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #808080; font-style: italic;">&lt;!-- Gets copied to target/.../WEB-INF/log4j.xml --&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;customLog4jXconf<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>etc/log4j.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/customLog4jXconf<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/configuration<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Finally, you need to arrange for the auto-generated <code>web.xml</code> to be patched with a reference to Log4jConfigListener.  This is done through Cocoon&#8217;s slightly arcane mechanism, <a href="http://cocoon.apache.org/2.2/maven-plugins/maven-plugin/1.0/1360_1_1.html">xpatch</a>.  Create a file <code>src/main/resources/META-INF/cocoon/xpatch/log4j.xweb</code> which looks like this.</p>

<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;">  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;xweb</span> <span style="color: #000066;">xpath</span>=<span style="color: #ff0000;">&quot;/web-app&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">unless</span>=<span style="color: #ff0000;">&quot;comment()[contains(., 'Log4j Configuration')]&quot;</span></span>
<span style="color: #009900;">        <span style="color: #000066;">insert-after</span>=<span style="color: #ff0000;">&quot;node()[1]&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
    <span style="color: #808080; font-style: italic;">&lt;!--Log4j Configuration--&gt;</span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>log4jConfigLocation<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/WEB-INF/log4j.xml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/param-value<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/context-param<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.springframework.web.util.Log4jConfigListener<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener-class<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/listener<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/xweb<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>

<p>Now if you run <code>mvn jetty:run</code> in your block and inspect the generated <code>web.xml</code>, you should see the above patched in to place.  Also, you should be able to generate messages on the console from within FlowScript by doing:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">  cocoon.<span style="color: #660066;">log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;hello world&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>The procedure above is a hassle.  But the benefit of being able to see logging messages coming out on the console in front of you is significant.</p>
<p>One final point to note.  When you do run <code>mvn jetty:run</code>, you&#8217;ll see a few log4j errors, i.e.</p>
<blockquote>
<p>log4j:WARN No appenders could be found for logger (org.apache.commons.configuration.ConfigurationUtils).<br />
log4j:WARN Please initialize the log4j system properly.</p>
<p>log4j:WARN No appenders could be found for logger (org.apache.commons.jci.stores.MemoryResourceStore).<br />
log4j:WARN Please initialize the log4j system properly.</p>
</blockquote>
<p>As far as I can tell these are completely ignorable, just very annoying.  They appear to happen before jetty itself starts up, and are irrelevant to the web app (as far as I can see).</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/08/27/logging-in-cocoon-22/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Tomcat Logging in WTP</title>
		<link>http://happygiraffe.net/blog/2008/08/01/tomcat-logging-in-wtp/</link>
		<comments>http://happygiraffe.net/blog/2008/08/01/tomcat-logging-in-wtp/#comments</comments>
		<pubDate>Fri, 01 Aug 2008 15:52:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[eclipse]]></category>
		<category><![CDATA[happygiraffe]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[tomcat]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2008/08/01/tomcat-logging-in-wtp/</guid>
		<description><![CDATA[I&#8217;ve just been trying to enable debug logging for tomcat (don&#8217;t ask). Normally you do this by editing $CATALINA_HOME/conf/logging.properties and restarting. Except I tried that in Eclipse (using WTP) and it didn&#8217;t work. I tried copying it to the $CATALINA_BASE/conf &#8230; <a href="http://happygiraffe.net/blog/2008/08/01/tomcat-logging-in-wtp/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just been trying to enable debug logging for tomcat (don&#8217;t ask).  Normally you do this by editing <code>$CATALINA_HOME/conf/logging.properties</code> and restarting.</p>
<p>Except I tried that in Eclipse (using <a href="http://www.eclipse.org/webtools/"><span class="caps">WTP</span></a>) and it didn&#8217;t work.</p>
<p>I tried copying it to the <code>$CATALINA_BASE/conf</code> directory instead<sup><a href="#fn1">1</a></sup>.</p>
<p>Still no joy.</p>
<p>I&#8217;ve just found the answer, after looking in the <a href="http://svn.apache.org/repos/asf/tomcat/tc6.0.x/trunk/">tomcat source</a>.  It turns out that tomcat&#8217;s alternative logging implementation (JULI) is enabled via a system property.  This happens inside <code>catalina.sh</code>:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #666666; font-style: italic;"># Set juli LogManager if it is present</span>
<span style="color: #000000; font-weight: bold;">if</span> <span style="color: #7a0874; font-weight: bold;">&#91;</span> <span style="color: #660033;">-r</span> <span style="color: #ff0000;">&quot;<span style="color: #007800;">$CATALINA_BASE</span>&quot;</span><span style="color: #000000; font-weight: bold;">/</span>conf<span style="color: #000000; font-weight: bold;">/</span>logging.properties <span style="color: #7a0874; font-weight: bold;">&#93;</span>; <span style="color: #000000; font-weight: bold;">then</span>
  <span style="color: #007800;">JAVA_OPTS</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$JAVA_OPTS</span> -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager&quot;</span>
  <span style="color: #007800;">LOGGING_CONFIG</span>=<span style="color: #ff0000;">&quot;-Djava.util.logging.config.file=<span style="color: #007800;">$CATALINA_BASE</span>/conf/logging.properties&quot;</span>
<span style="color: #000000; font-weight: bold;">fi</span></pre></div></div>

<p>But Eclipse runs the Java code directly, without using <code>catalina.sh</code>.  So the properties never gets set.  You have to set them by hand in the &#8220;Run Configurations&#8221; dialog.  Like this:</p>
<p><img src="http://happygiraffe.net/blog/files/eclipse-tomcat-system-properties.png" alt="Setting system properties for tomcat in Eclipse" border="0" width="563" height="520" /></p>
<p>Of note there is that I&#8217;ve imported the <code>logging.properties</code> file into the <code>Servers</code> project of my workspace.  It seemed liked a useful place to put it.</p>
<p>Of course, after realising that, I soon find the same information in the <span class="caps">WTP FAQ</span>: <a href="http://wiki.eclipse.org/WTP_Tomcat_FAQ#How_do_I_enable_the_JULI_logging_in_a_Tomcat_5.5_Server_instance.3F">How do I enable the <span class="caps">JULI</span> logging in a Tomcat 5.5 Server instance?</a>.</p>
<p>Anyway, now I might be able to debug that ClassLoader issue&#8230;</p>
<p id="fn1"><sup>1</sup>  Inside Eclipse with <span class="caps">WTP</span>, that will be something like <code>$WORKSPACE/.metadata/.plugins/org.eclipse.wst.server.core/tmp0</code>.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2008/08/01/tomcat-logging-in-wtp/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Log Rotation</title>
		<link>http://happygiraffe.net/blog/2006/07/27/log-rotation/</link>
		<comments>http://happygiraffe.net/blog/2006/07/27/log-rotation/#comments</comments>
		<pubDate>Thu, 27 Jul 2006 00:22:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[apache]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[postgresql]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2006/07/27/log-rotation/</guid>
		<description><![CDATA[I hate log rotation. It&#8217;s a pain to configure on my FreeBSD server. Just look at newsyslog.conf. That, and my experiences of the utter non-portability of log rotation programs between different Unixes have led me to believe that programs should &#8230; <a href="http://happygiraffe.net/blog/2006/07/27/log-rotation/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>I hate log rotation.  It&#8217;s a pain to configure on my FreeBSD server.  Just look at <a href="http://www.freebsd.org/cgi/man.cgi?query=newsyslog.conf&#38;apropos=0&#38;sektion=5&#38;manpath=FreeBSD+6.1-RELEASE&#38;format=html" title="5">newsyslog.conf</a>.  That, and my experiences of the utter non-portability of log rotation programs between different Unixes have led me to believe that programs should probably handle their own log rotation.  It just makes life easier having one less thing to integrate with the Operating System.</p>
<p>So, I&#8217;ve switched my Apache over to using <a href="http://cronolog.org/">cronolog</a> in order to get date based logfiles automatically.  I&#8217;ve used it on a project at work recently and it really works a treat.</p>
<p>I also noticed that <a href="http://www.postgresql.org/">PostgreSQL</a> has grown the ability to <a href="http://www.postgresql.org/docs/8.1/static/runtime-config-logging.html">take care of its own log files</a> in recent versions.  So I&#8217;ve switched that to doing date based logging with automatic rotation as well.  Lovely.</p>
<p>All is not perfect of course.  Oh no, that would be far too simple.  There&#8217;s still the issue of removing old logfiles and/or compressing them.  But that seems to be a smaller integration problem.</p>
<p>Of course the trigger for all this activity was finding a 220Mb <code>access_log</code> lying around.  Doh!</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2006/07/27/log-rotation/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Log::Dispatch::Atom 0.02</title>
		<link>http://happygiraffe.net/blog/2005/11/18/log-dispatch-atom-0-02/</link>
		<comments>http://happygiraffe.net/blog/2005/11/18/log-dispatch-atom-0-02/#comments</comments>
		<pubDate>Fri, 18 Nov 2005 07:57:00 +0000</pubDate>
		<dc:creator>Dominic Mitchell</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[atom]]></category>
		<category><![CDATA[logging]]></category>
		<category><![CDATA[perl]]></category>

		<guid isPermaLink="false">http://happygiraffe.net/2005/11/18/log-dispatch-atom-0-02/</guid>
		<description><![CDATA[The other day, I released]]></description>
			<content:encoded><![CDATA[<p>The other day, I released <a href="http://search.cpan.org/<sub>hdm/Log-Dispatch-Atom-0.02/lib/Log/Dispatch/Atom.pm&#8221;&gt;Log::Dispatch::Atom</a>.  Originally, I just wanted to log messages to an <a href="http://atomenabled.org/">Atom</a> file, so I could get my web app to record errors that I can pick up through my feed reader.  But I haven&#8217;t gotten around to implementing that yet.</p>
<p>About half way through writing it, I remembered the <a href="http://search.cpan.org/</sub>drolsky/Log-Dispatch-2.11/lib/Log/Dispatch.pm&#8221;&gt;Log::Dispatch</a> module.  So, when I was happy that the module was working OK, I refactored it to be usable by Log::Dispatch (which was very simple&#8212;a testament to the simple design of Log::Dispatch::Output).</p>
<p>Anyway, I&#8217;ve now released Log::Dispatch::Atom to <span class="caps">CPAN</span>, for all your feedy needs.  Almost.  At the moment, it&#8217;s still a bit too easy to create Atom feeds that the <a href="http://feedvalidator.org/">feed validator</a> doesn&#8217;t like.  I need to have a bit of a think about the best way to get things like &#8220;id&#8221; in there for each log message.  But it should still be usable for now.</p>
]]></content:encoded>
			<wfw:commentRss>http://happygiraffe.net/blog/2005/11/18/log-dispatch-atom-0-02/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

