Managing logging with Log4j and Chainsaw

Those of you who haven’t used System.out.println(”My variable: ” + myVariable) in some Java classes to get debug information to the console, raise your hands please. None? 😉

I am very fond of logging as a complement to debugging, since I can ”trace” actions in my programs even if the classes is in production. ”But hey, logging makes the application go slower!” you might think. Well, it’s up to you to decide, a small performance hit is likely to occur, a big hit if you don’t read the documentation of for instance the PatternLayout.
I use Log4j a lot in the Java code I design and write, and haven’t noticed any big difference between logging and not logging.
So, you might have used Log4j a bit and used Chainsaw to look at Log4j logging files. Why not look at the logging in realtime? Here is how to do it.

Setting up Log4j

To see the logging in real time, I use an appender called SocketHubAppender that acts as a server. Create the log4j.xml in the root of your Java application’s directory, or add the vital bits to your own:

< ?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE log4j:configuration SYSTEM "log4j.dtd">





When starting your application and outputs some logging messages with the usual logger.debug(”Hello world!”); it will be sent to the appender’s socket, defined above as 1020. The output can’t be read using a telnet session, because the things you will see is the logging events serialized… You need something to look at those serialized logging events with – Chainsaw!

Configure Chainsaw

Create a file (or add the vital bits below to your own…) called chainsaw-config.xml somewhere on your local disk, for instance c:tempchainsaw-config.xml, and change the host from my.remote.host.net to the host where you set up the logging with the SocketHubAppender and that the port matches the one you used:

< ?xml version="1.0" encoding="UTF-8" ?>
< !DOCTYPE log4j:configuration >






Start Chainsaw via Java Web Start by launching the jnlp-file at the homepage. Open the menu ”View”, ”Show Application-wide Preferences”, go to the ”General” tab and enter ”file://c:/temp/chainsaw-config.xml” (leave out the quotation marks) in the ”Automatic Configuration URL” field, or whatever you named your config file. Click ”OK” and restart Chainsaw. You should now see your receiver to the right named ”MyRemoteLogging”.
Try starting up your Java application and output some logging, and you will see them pop up directly in Chainsaw!

So why not use a simple file or database to log to? You can do that too, you are not limited to one appender per application. You can set up as many as you want, for instance a ConsoleAppender, a SocketHubAppender and a JDBCAppender that logs the events to all those targets.
Log4j is powerful, but it doesn’t makes miracles if you don’t use some structured logging. Events like ”Hello world!” is not going to help you, but some well designed NDC.push()’s and other will do good. And don’t forget to add the NotesExceptionRenderer to your class path, so that you get more info from those badly-designed exceptions 🙂