collections.jar
Logging is especially useful during development and testing since it is an invaluable debugging aid, but also during production runs, where a certain amount of logging is often needed to supervise a program.
There are a few things that have to be considered to get the most out of logging:
The Logger class is responsible for generating log messages as well as for suppressing that generation, while the Log class is responsible for filtering and dispatching log messages.
Log messages can be redirected to a log file with the help of the {@link de.qfs.lib.log.FileLogWriter FileLogWriter} class or sent to a separate LogServer process with a {@link de.qfs.lib.logrmi.RemoteLogWriter RemoteLogWriter}.
Typical use of the log package looks like this:
import de.qfs.lib.Log; import de.qfs.lib.Logger; class SomeClass { // Initialize the logger for SomeClass private final static Logger logger = new Logger (SomeClass.class); public void someMethod(int someParameter) { // log entry to the method, optionally with parameters if (logger.level >= Log.MTD) { logger.log(Log.MTD, "someMethod(int)", logger.level < Log.MTDDETAIL ? "" : "someParameter: " + someParameter); } ... // some debugging output if (logger.level >= Log.DBG) { logger.log(Log.DBG, "someMethod(int)", "Some debug message"); } ... try { // do something dangerous ... } catch (BadException ex) { // log exception at error level if (logger.level >= Log.ERR) { logger.log("someMethod(int)", ex); } } catch (NotSoBadException ex) { // log exception at warning level if (logger.level >= Log.WRN) { logger.log(Log.WRN, "someMethod(int)", ex); } } } }While the above may look overly complicated, it's not that bad if you consider the benefits:
if (logger.level >= ...)
protection the
creation of the log message can be prevented very early, before the
Strings of the message are generated and concatenated, which is the
most expensive part. While this could be made even more effective by
using a final static
flag and letting the compiler remove
the log call altogether, this doesn't make a noticable difference, even
with a lot of logging going on, but is very inflexible.