Logging APIs – Feature List : Dragons in the Algorithm

Logging APIs – Feature List

Logging is not the world’s most interesting computing problem, but it is important, and it’s been on my mind lately because people have been pointing out that my company’s use of logging is currently a bit of a mess and ought to be cleaned up. Specifically, I’ve been thinking about the API that is used to invoke logging and also a little bit about the plumbing that is responsible for doing something with the log messages. What I would like to do here is a bit too big for one entry, so I will split it in two. In this, the first part, I will simply catalog some features that a logging API can have, then in the second part I will use that list to evaluate a few Java libraries. The catalog of features would apply to any language, but I will consistently use Java as an example.

The ultimate goal in logging is to write out some messages that are solely for use in debugging and managing a system — output that is a side effect rather than the primary goal of the program. The simplest version of logging has always been this:

SplineList splineList = reticulateSplines(rawSplines);
System.out.println(splineList);

The simple “print” statement is a universal form of logging (I can name only a couple of languages that don’t have some basic syntax to support it) and I have used it far more times than I would choose to admit. Any logging framework should be evaluated in terms of what it adds to this capability. Here are the features I have been able to think of:

So that’s my list of features; see my next entry where I use this list of features to evaluate some Java logging libraries.

Comments

3 Responses to “Logging APIs – Feature List”

  1. David Horvath on 2010-02-09 9:19 am

    Two issues largely missed in logging

    Uniqueness of message – “we had an error” that appears in multiple places… Which specific chunk of code with that message was the one that created it? I’ve seen function name and/or unique error number be most effective. But if you’ve got multiple functions with the same name (object overloading) then it can become complex. And tracking unique error numbers, especially with shared code/object libraries can become complex.
    Message buffering. This one only really matters if the application crashes rather than being able to exit gracefully when an error is detected/handled. I’m a strong proponent of the C fflush(); it could be handled as part of the error logging code/objects (your logger.X example) with a frequency count (every N messages) or semi-explicitly (after a type of invocation), or even manually.

    Granted, frequent fflushes slows processing (forcing physical I/O rather than buffered into larger writes), but provides more information on failure.

    Your last “Delayed String Construction” example reminds me a lot of the C printf statement… Except you used {} instead of %s%s%s%s%s.

  2. mcherm on 2010-02-09 9:20 am

    Oh, I particularly like your #1 “Uniqueness of Message”. A useful feature that I’ve seen is to automatically mark each log message with the line and file from which it was generated. This should be one of the criteria.

    #2 I’m not so sure about. In the Java world, files flush when they are closed suddenly, unless the JVM itself crashes — and that is VERY rare these days; it essentially never happens with our files. Of course, this isn’t supposed to be a java-specific list, so given that I should probably list that one too.

  3. David Horvath on 2010-02-09 9:21 am

    The JVM may prevent #2 but that doesn’t help for O/S faults, or, as you mention, other languages. In the “old C days” the saying was “The world is not a VAX” (because the VAX would let you get away with things that would upset other systems – like dereferencing the NULL pointer). So you have to remember, “The world is not just Java” :-)

Leave a Reply