Good examples using java.util.logging

java, java.util.logging, logging

Solution

`java.util.logging` keeps you from having to tote one more jar file around with your application, and it works well with a good Formatter.

In general, at the top of every class, you should have:

private static final Logger LOGGER = Logger.getLogger( ClassName.class.getName() );

Then, you can just use various facilities of the Logger class.

Use `Level.FINE` for anything that is debugging at the top level of execution flow:

LOGGER.log( Level.FINE, "processing {0} entries in loop", list.size() );

Use `Level.FINER` / `Level.FINEST` inside of loops and in places where you may not always need to see that much detail when debugging basic flow issues:

LOGGER.log( Level.FINER, "processing[{0}]: {1}", new Object[]{ i, list.get(i) } );

Use the parameterized versions of the logging facilities to keep from generating tons of String concatenation garbage that GC will have to keep up with. `Object[]` as above is cheap, on the stack allocation usually.

With exception handling, always log the complete exception details:

try {
    ...something that can throw an ignorable exception
} catch( Exception ex ) {
    LOGGER.log( Level.SEVERE, ex.toString(), ex );
}

I always pass `ex.toString()` as the message here, because then when I "`grep -n`" for "`Exception`" in log files, I can see the message too. Otherwise, it is going to be on the next line of output generated by the stack dump, and you have to have a more advanced RegEx to match that line too, which often gets you more output than you need to look through.

Problem

I want to use logs in my program. I heard about java.util.logging, but I don't know how to begin. Are there any examples of what can I do with logging? How would I use logging in my own program?

Original source

Related problems