Newsletter sign-up
View all newsletters

Sign up for our technology specific newsletters.

Enterprise Java
Email Address:

Write custom appenders for log4j

Extend log4j to support lightweight over-the-network logging

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Logging is the simple process of printing messages of various types to known places. Logging messages can go to a console, to a file, to a remote monitor, or anywhere else that you find convenient. Think of logging as a sophisticated sibling of:

 if( debug )
    System.out.println("Debugging diagnostic");



Logging has several advantages over simple println() statements, however. The logging system can add contextual information—filename, line number, and date, for example—to the message automatically. You can redirect the messages to different destinations, or change the formatting, without recompiling your program. (In log4j, you just modify a simple properties file.) You can easily turn categories of messages on and off so you can see debug messages when you're debugging, but easily turn them off when you're not, for example.

Logging is central to all of my programs. I use it to monitor my program's progress as it works. I use it to log error messages from library methods that might be used in a server-side context (where there's no console on which to print a stack trace). Most importantly, logging is one of my main debugging tools. Though visual debuggers are handy on occasion, I've noticed that I can find more bugs faster by combining a careful reading of the code with a few well-placed logging messages. (I'm not quite sure why reading/logging seems more effective than visual debugging, but my current theory is that a visual debugger narrows your focus to a single thread of control through the program, so you tend to miss bugs that aren't on that thread.)

Logging is essential in server-side debugging, where, typically, no console exists, so System.out proves useless. For example, Tomcat sends System.out to its own log file, so you never see messages that are sent there unless you have access to that log file. More to the point, you probably want to monitor a server's performance from somewhere other than the server itself. Checking server logs is nice, but I'd much rather see the logs on my workstation.

One of the better logging systems around is the Apache Software Foundation's log4j project. It's more flexible and easier to use than Java's built-in APIs. It's also a trivial install—you just put a jar file and a simple configuration file on your CLASSPATH. (Resources includes a good introduction article to log4j.) Log4j is a free download. The stripped-down but adequate-for-the-end-user documentation is also free. But you have to pay 0 for the complete documentation, which I recommend.

This article will look at how to extend log4j by adding a new appender—the part of the system responsible for actually sending the log messages somewhere. The appender I discuss is a lightweight version of the socket-based appender that comes with log4j, but you can easily add your own appenders to put log messages into a database or LDAP (lightweight directory access protocol) directory, wrap them in proprietary protocols, route them to specific directories, and so forth.

  • Digg
  • Reddit
  • SlashDot
  • Stumble
  • del.icio.us
  • Technorati
  • dzone
Comments (5)
Login
Forgot your account info?

log4j resultsBy Anonymous on November 1, 2009, 3:26 amHi. I'm using a tool for monitoring the memory usage of my cpu called nsclient4j(java api). It required the log4j tool and I added it into my netbeans jar folder....

Reply | Read entire comment

Custom performance loggerBy Anonymous on October 13, 2009, 6:43 pmThere is some readymade code @ http://diggintojava.blogspot.com/

Reply | Read entire comment

An online web Log4j viewerBy Shlomo Schwarcz on August 5, 2009, 8:42 amHere is an interesting project I developed: a log4j generic viewer written purly in Java Script. Simply paste your log and the log4j pattern and the results will...

Reply | Read entire comment

need to log events into a jspBy Anonymous on July 14, 2009, 10:08 amHi, I need to log events into a jsp file. I sit possible to write a custom file appender for this. Can anyone please help in this

Reply | Read entire comment

log4j viewer and analysisBy Anonymous on November 19, 2008, 3:58 amGreat article, we also use XpoLog Log Viewer and analysis to process and monitor our log4j logs. http://www.xpolog.com or http://www.log-viewer.com

Reply | Read entire comment

View all comments

Add comment
Anonymous comments subject to approval. Register here for member benefits.
Have a JavaWorld account? Log in here. Register now for a free account.
Resources