JavaOne 2006 Submission: Logging with java.util.logging

Since I spent sometime a couple months ago putting together a presentation on logging in Java, I thought I’d submit a proposal to JavaOne this year. Here’s the proposal I wrote up:

Title

Logging in Java ™ with java.util.logging

Abstract

This presentation shows you how and why to use the logging API that’s
bundled in the core J2SE library. You’ll learn the basics of logging
and configuring, advanced logging and configuration, how to tailor
logging to your needs, and best practices for logging.

We’ll go beyond the standard documentation available in the J2SE and
on the web, using plenty of examples to explain pragmatic ways to
quickly and effectively use the logging API in your code.

The built in logging API has been available in the J2SE for several
years, but consistent and knowledge use of this API is sparse. This
despite both the richness and simplicity of logging, not to mention
the consistency and portability benefits of using the bundled logging
API.

Logging is vital for both quick debugging during development and once
your software has been deployed onto hosted servers or in the field to
customers. Oftentimes all that stands between you and hours of
debugging over the phone is the ability to quickly use logging to
diagnose problems remotely. Attending this session will help save you
from those tedious and costly hours, keeping you and your customers
happy.

Presentation Summary

The presentation follows the outline below:

What logging code looks like

A quick dive into what logging with the logging API actually looks like in
code and as output.

When and why to log

Why programmers should bother to thoroughly put logging in their applications:
the primary reason being that once your application is run in a non-developer
environment, in production, it becomes almost impossible to debug. As a result,
customers get frustrated as problems take “forever” to fix, and companies spend
too much time and money diagnosing problems in the field.

The Logging API

This section introduces the different concepts in the logging API: log
levels, the different log messages, handlers, formatters, etc.

It also covers best practices in logging, such as when to double check
the logging level for performance, using argument’ed messages, logging
exceptions, and how to determine where in the code to log.

Finally, this section covers how to configure logging using the
default flat files, at runtime, and with the newly available logging
MBean. Knowing how to properly configure logging, especially during
runtime is an invaluable skill.

This section contains many code examples that illustrate the points.

Customizing Logging

This section covers the customizations that can be made in the logging
API, such as custom handlers and formatters, as well as some
extensions to logging, such as context loggers that take care of
logging tedious to code, but vital to debug, information.

A brief section covers using Aspect Oriented Programming to help weave
logging in; pit-falls of an AOP approach are also covered.

Logging Grab Bag, Tip and Tricks

The last section collects together a “Top 10” style list of tips and tricks
with the logging API.

If you’re interested in taking a look at it, there’s a draft of the presentation available.

And, of course, if you’re interested in me giving the presentation, feel free to contact me and we’ll work something out: I’m sure it’d be fun ;>

Tags: , , , .

Join the Conversation

5 Comments

  1. Hey cote
    Great ppt you got, I like the slide about the micro-optimizations it make me rethink my all logging.

    I use isLoggable() and a Object[] in my code now I realized that I rather use Stringbuffer to build the msg …

    did I got it right ? is it cheapter to use StringBuffer for the msg assuming there is a isLoggable() before the log ?

    thanks
    gil p

  2. I typically don’t use StringBuffers for log messages, I just use the argument’ed message with an Object array for arguments. My assumptions are that if you’re using an isLoggable, you’re not too concerned about the performance hit (if any) of what happens in the block. That is, your assumption is that you’d rather get the logging information than performance in that case.

  3. hi Cote,

    I am using java.util.Logging.logger in my class, by default the messages are printed in the console,Can u tell me how to print all the logged messages in to a seperate file.

    regards
    Ragu

  4. Sure, you need to open up logging.properties and uncomment out the FileHandler example that’s in there.

    That file should be located at /jre/lib and just look around for “file” or “FileHandler.”

    If not, search on google for something like “java file handler” and you should find some examples of configuring it.

  5. Hi Cote,

    This post sounds really interesting but the link to your presentation seems to be dead. Would you mind reposting the ppt/pdf of your slides.

    Cheers,
    Kaushik

Leave a comment

Leave a Reply to Ragunanthan Cancel reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.