Thursday, March 11, 2004

Software Tools: log4j standardizes logging, eliminates rework, and improves performance

Every great developer I know bends over backwards not to write code. Every line you write has to be maintained for the rest of your life (or someone else's life). Yet how many developers in your organization are writing, rewriting, and debugging logging code. And who really has time to optimize performance for this low level stuff.

Object technology is supposed to enable components which should increase productivity. Open source has made the software free. Not only that, for code that is written over and over again, large numbers of open source programmers make the software better than any individual programmer can write. Of course, you may be the exceptional programmer that can write code better than anyone on the planet. But should you apply your extreme added value to write and maintain logging code? I don't think so. If you do you will be outsourced! A lot of outsourcing shops are probably already using log4j.

Why do logging at all? Let's look at what Kernigan has to say about it:

As personal choice, we tend not to use debuggers beyond getting a stack trace or the value of a variable or two. One reason is that it is easy to get lost in details of complicated data structures and control flow; we find stepping through a program less productive than thinking harder and adding output statements and self-checking code at critical places. Clicking over statements takes longer than scanning the output of judiciously-placed displays. It takes less time to decide where to put print statements than to single-step to the critical section of code, even assuming we know where that is. More important, debugging statements stay with the program; debugging sessions are transient.

Kernigan, Brian and Pike, Rob. The Practice of Programming. Addison-Wesley, 1999.

Check out the log4j apache site.

Inserting log statements into your code is a low-tech method for debugging it. It may also be the only way because debuggers are not always available or applicable. This is often the case for distributed applications.

On the other hand, some people argue that log statements pollute source code and decrease legibility. (We believe that the contrary is true). In the Java language where a preprocessor is not available, log statements increase the size of the code and reduce its speed, even when logging is turned off. Given that a reasonably sized application may contain thousands of log statements, speed is of particular importance.

With log4j it is possible to enable logging at runtime without modifying the application binary. The log4j package is designed so that these statements can remain in shipped code without incurring a heavy performance cost. Logging behavior can be controlled by editing a configuration file, without touching the application binary.


Post a Comment

<< Home