if( debug )
System.out.println("Debugging diagnostic");
Logging has several advantages over simple
println()
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.
| Subject |
|
|
|
|
|
|
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