In many Java programming projects, logging is one of those “eat your vegetables” kind of tasks: you have every every intention of doing it, but somehow never manage to. Instead, if any logging is in your code, it’s the remnants of a few System.out.println() statements you forgot to remove after debugging a class.
In many Java programming projects, logging is one of those “eat your vegetables” kind of tasks: you have every every intention of doing it, but somehow never manage to. Instead, if any logging is in your code, it’s the remnants of a few System.out.println() statements you forgot to remove after debugging a class.
Even if you consider logging “spaghetti code debugging”, logging does have its place. In some situations, it is difficult, sometimes impossible, to use traditional debugging techniques. Unfortunately, putting temporary code into a Java program is normally problematic. After all, Java doesn’t support conditional compilation.
(If you really want temporary code in your Java class, try this gimmick: put logging statements inside an if block with a static Boolean variable as the condition. While you’re debugging, set the variable to true; when you’re done, set it to false. Optimizing compilers can figure out that the code in the if statement can never be executed, and will omit it from the final bytecode.)
Jakarta (http://jakarta.apache.org), a Java development project of the Apache Software Foundation (best known for its Tomcat Web server and Ant build tool), offers a set of classes that makes logging in Java easy. Log4j, which is distributed under the Apache Software License, is a six-year-old effort to enable logging in Java. Log4j supports a large number of output formats, does not significantly impact the performance of your code, and logging can be turned on or off at runtime.
Apache…
Please log in to view this content.
Not Yet a Member?
Register with LinuxMagazine.com and get free access to the entire archive, including: