Java Logging




Java Logging is a logging implementation that is part of the JDK. Other popular options include Log4J and Jakarta Commons Logging. An advantage of using this instead of Log4J is that it avoids potential JAR version conflicts which can occur if you use a different version of Log4J than a library you are using that expects/includes a different version.

Installing top

There is nothing to be installed and nothing to put in the classpath since this is included in the JDK.

Usage top

The basic steps to use Java Logging are:

  1. Configure Java Logging.
  2. In each Java class that needs logging,
    1. Obtain a Logger object.
    2. Invoking logging methods on it.

Configuration top

Java Logging can be configured in three ways.

  1. from Java code
  2. from $JAVA_HOME/lib/
  3. from a property file located elsewhere
The last option is typically preferred.

The configuration property file must be specified by setting the system property java.util.logging.config.file. This can be done when an application is run by specifying -Djava.util.logging.config.file={file-path} on the java command. When running from ant, add the following as a child of the java task.
<sysproperty key="java.util.logging.config.file" value="{file-path}"/>

Here's an example of a configuration file, named

# Add handlers to the root logger. # These are inherited by all other loggers. handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler # Set the logging level of the root logger. # Levels from lowest to highest are # FINEST, FINER, FINE, CONFIG, INFO, WARNING and SEVERE. # The default level for all loggers and handlers is INFO. .level=SEVERE # Specify logging levels for specific namespaces. com.ociweb.level=FINEST # Configure the ConsoleHandler. # ConsoleHandler uses java.util.logging.SimpleFormatter by default. # Even though the root logger has the same level as this, # the next line is still needed because we're configuring a handler, # not a logger, and handlers don't inherit properties from the root logger. java.util.logging.ConsoleHandler.level=SEVERE # Configure the FileHandler. # FileHandler uses java.util.logging.XMLFormatter by default. java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.FileHandler.level=FINEST # The following special tokens can be used in the pattern property # which specifies the location and name of the log file. # / - standard path separator # %t - system temporary directory # %h - value of the user.home system property # %g - generation number for rotating logs # %u - unique number to avoid conflicts # FileHandler writes to %h/demo0.log by default. java.util.logging.FileHandler.pattern=demo.log

Example top

Here's an example of using Java Logging in Java code. Note how a Logger instance is obtained.

package com.ociweb.demo; import java.util.logging.Logger; public class LoggingDemo { private Logger logger = Logger.getLogger(getClass().getName()); public static void main(String[] args) { LoggingDemo app = new LoggingDemo(); // To do something only if logging is set at or below a given level, // such as construct a complicated log message ... //if (logger.isLoggable(Level.{constant})) { ... } app.doSomething(); } public LoggingDemo() { logger.finest("message 1"); } public void doSomething() {"message 2"); } }

In this example, using the configuration shown earlier, the file demo.log is created in the current directory with the following contents. Note how the method name reported for logging from a constructor is <init>.

Feb 20, 2007 9:14:24 AM com.ociweb.demo.LoggingDemo <init> FINEST: message 1 Feb 20, 2007 9:14:24 AM com.ociweb.demo.LoggingDemo doSomething INFO: message 2

Methods in the Logger class for writing log messages, in order from lowest to highest severity, include the following. Each of these has no return value and takes a String parameter that is either a log message or a key in a message catalog. What is a message catalog? Perhaps it's a ResourceBundle described here.

The log method can be used to write a log message for a given level and it takes additional parameter for constructing a log message using the toString value of an arbitrary number of objects.

Methods in the Logger class for obtaining information about the current level of the Logger include the following. These are commonly used to avoid construction of a complicated log message when it will not be output.

Copyright © 2007 Object Computing, Inc. All rights reserved.