Tutorials : Looking into the JDK 1.4 Logging API :

Showing the logging set-up

First we'll code a helper class that can reveal any application's use of loggers, handlers and formatters. To get this information we use a singleton class from the logging API--the LogManager--which administers all the active loggers. The helper class is called LoggerInformation LoggerInformation , and if you click on the link, you will see the Java source code in a separate window.

The LoggerInformation class works like this:

  • get the LogManager instance
  • using this instance get all the logger names...
  • ... and all the loggers, and for each of them:
    • get level, filter, parent
    • get all handlers, and for each of them:
      • get name, formatter, level, filter

LoggerInformation simply writes the information to System.out. The tryLevels method at the bottom of the class can be used to send all the standard message types to a given logger.

First we'll use the LoggerInformation class to show us the default logging setup. For this purpose we simply use a program that doesn't define any loggers explicitly:

package hansen.playground.logging;
    
    import java.util.*;
    import java.util.logging.*;
    
    public class Log0 {
      public static void main(String[] args) {
        // Show info for default loggers
        LoggerInformation.getInfo();
      }
    }

If we run this program we get this written to the console:

***Begin Logger Information
    -----------------------
    Logger name: >global<
    Logger level: null
    No filter used
    No handlers defined
    Parent: ><
    -----------------------
    Logger name: ><
    Logger level: INFO
    No filter used
    Handlers:
    java.util.logging.ConsoleHandler
      using formatter: java.util.logging.SimpleFormatter
      using level: INFO
      no filter
    No parent
    *** End Logger Information

What we can see is this:

  • 2 loggers are defined, "global" and the "root" logger. The "root" logger has no name.
  • the "global" logger has no defined level, so it inherits the level--INFO--from its parent: the "root" logger.
  • the "global" logger hasn't got a handler defined either, so the "root" logger's handler will be used.
  • the "root" logger has a ConsoleHandler defined which uses a SimpleFormatter.
  • no filters are defined for the two loggers.

Now let's add a single logger to our program:

package hansen.playground.logging;
    
    import java.util.*;
    import java.util.logging.*;
    
    public class Log1 {
      private static Logger logger1 =
        Logger.getLogger(Log1.class.getName());
    
      public static void main(String[] args) {
        LoggerInformation.getInfo();
        LoggerInformation.tryLevels(logger1);
      }
    }

Note, that I've added a call to tryLevels. This time we'll get this on the console:

***Begin Logger Information
    -----------------------
    Logger name: >global<
    Logger level: null
    No filter used
    No handlers defined
    Parent: ><
    -----------------------
    Logger name: >hansen.playground.logging.Log1<
    Logger level: null
    No filter used
    No handlers defined
    Parent: ><
    -----------------------
    Logger name: ><
    Logger level: INFO
    No filter used
    Handlers:
    java.util.logging.ConsoleHandler
      using formatter: java.util.logging.SimpleFormatter
      using level: INFO
      no filter
    No parent
    *** End Logger Information
    11-08-2002 11:30:04 hansen.playground.logging.LoggerInformation
      tryLevels
    SEVERE: severe message
    11-08-2002 11:30:05 hansen.playground.logging.LoggerInformation
      tryLevels
    WARNING: warning message
    11-08-2002 11:30:05 hansen.playground.logging.LoggerInformation
      tryLevels
    INFO: info message

This time our own logger--hansen.playground.logging.Log1--has been added to the list. Since we haven't defined anything for the logger it inherits its level and uses the handler from the "root" logger--as "global" did.

The messages with a level of at least "INFO" are the only ones written to the console.

How to Add Java Applets to Your Site

New on the Java Boutique:

New Review:

Time Management Made Easy with the Quartz Enterprise Job Scheduler
Why not just use the Java timer API? This open source scheduling API boasts simplicity, ease-of-integration, a well-rounded feature set, and it's free!

New Applet:

Reverse Complement
Reverse Complement is a simple applet that converts DNA or RNA sequences into three useful formats.

Elsewhere on internet.com:

WebDeveloper Java
Lots of Java information on webdeveloper.com

WDVL Java
Thorough Java resource at the Web Developer's Virtual Library.

ScriptSearch Java
Hundreds of free Java code files to download.

jGuru: Your View of the Java Universe
Customizable portal with online training, FAQs, regular news updates, and tutorials.