February 17, 2010

Viewing/Monitoring your Log4j Generated Log File with Chainsaw

Chainsaw is a open source tool for viewing and/or monitoring Log4j generating system. But the documentation on the homepage lack some explanation how to get started. To begin with maybe the simplest scenario, to view a log4j generated log file. The first thing one might misunderstand is that you need to defines a new configuration file for Chainsaw, so just pointing to your log4j.xm configuration file is not enough. Lets start with a example.

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

 <appender name="FILE_APPENDER" class="org.apache.log4j.RollingFileAppender">
  <param name="File" value="/tmp/server.log" />


  <param name="Append" value="true" />


  <layout class="org.apache.log4j.PatternLayout">
   <param name="ConversionPattern" value="%d %-5p [%t] %C{2} (%F:%L) - %m%n" />


  </layout>
 </appender>

 <logger name="se.msc.example.log4j.ChainsawTest">
  <level value="DEBUG" />
 </logger>

 <root>
  <level value="ERROR" />
  <appender-ref ref="FILE_APPENDER" />
 </root>

</log4j:configuration>


And the configuration file for Chainsaw.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration PUBLIC "-//log4j/log4j Configuration//EN" "log4j.dtd"> 
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="true">

   <plugin name="logFileReceiver" class="org.apache.log4j.varia.LogFilePatternReceiver">
     <param name="fileURL" value="file:///tmp/server.log" />
     <param name="timestampFormat" value="yyyy-MM-d HH:mm:ss,SSS"/>
     <param name="logFormat" value="TIMESTAMP LEVEL [THREAD] CLASS (FILE:LINE) - MESSAGE"/>
     <param name="name" value="Reciever-name-in-Chainsaw" />
     <param name="tailing" value="true" />
   </plugin>
   <root>
      <level value="debug"/>
   </root>
</log4j:configuration>


Before we open Chainsaw lets write a small test program, that generates some log post, so we have something to view.

package se.msc.example.log4j;

import org.apache.log4j.Logger;
import org.junit.Test;

public class ChainsawTest {
 private static final Logger log = Logger.getLogger(ChainsawTest.class);
 
 @Test
 public void test() throws Exception {
  log.debug("DEBUG text");
  log.info("INFO text");
  log.warn("WARN text");
  log.error("ERROR text");
 }
}


Now lets open Chainsaw. When first starting Chainsaw you are prompted to define a Receiver. Here we select our configuration file.



After selecting our Chainsaw configuration file, Chainsaw opens our log file in a new tab.



You might have already noticed the different formatting of logFormat attribute, compared with the Log4j and it is a question to me why they have done that. You can read more about the format in the LogFilePatternReceiver Javadoc.

No comments: