Recipe 17.9 Network Logging with JDK 1.4


You wish to write logging messages using the JDK 1.4 logging mechanism.


Get a Logger, and use it to log your messages and/or exceptions.


The JDK 1.4 Logging API (package java.util.logging ) is similar to, and obviously inspired by, the log4j package. You acquire a Logger object by calling the static Logger.getLogger( ) with a descriptive String. You then use instance methods to write to the log; these methods include:

public void log(java.util.logging.LogRecord); public void log(java.util.logging.Level,String); // and a variety of overloaded log(  ) methods public void logp(java.util.logging.Level,String,String,String); public void logrb(java.util.logging.Level,String,String,String,String); // Convenience routines for tracing program flow public void entering(String,String); public void entering(String,String,Object); public void entering(String,String,Object[]); public void exiting(String,String); public void exiting(String,String,Object); public void throwing(String,String,Throwable); // Convenience routines for log(  ) with a given level public void severe(String); public void warning(String); public void info(String); public void config(String); public void fine(String); public void finer(String); public void finest(String);

As with log4j, every Logger object has a given logging level, and messages below that level are silently discarded:

public void setLevel(java.util.logging.Level); public java.util.logging.Level getLevel(  ); public boolean isLoggable(java.util.logging.Level);

As with log4j, objects handle the writing of the log. Each logger has a Handler :

public synchronized void addHandler(java.util.logging.Handler); public synchronized void removeHandler(java.util.logging.Handler); public synchronized java.util.logging.Handler[] getHandlers(  );

and each Handler has a Formatter , which formats a LogRecord for display. By providing your own Formatter, you have more control over how the information being passed into the log gets formatted.

Unlike log4j, the 1.4 logging mechanism has a default configuration, so this is a minimal logging example program:

import java.util.logging.Logger; public class Log14Demo {     public static void main(String[] args) {         Logger myLogger = Logger.getLogger("com.darwinsys");         Object o = new Object(  );"I created an object: " + o);     } }

Running it prints the following:

C:> java Log14Demo Mar 8, 2004 7:48:26 PM Log14Demo main INFO: I created an object: java.lang.Object@57f0dc C:>

As with log4j, the typical use is in logging caught exceptions; the code for this is in Example 17-13.

Example 17-13. Log14Demo2 catching and logging
import java.util.logging.Logger; import java.util.logging.LogRecord; import java.util.logging.Level; public class Log14Demo2 {     public static void main(String[] args) {         Logger myLogger = Logger.getLogger("com.darwinsys");         try {             Object o = new Object(  );             if (o != null) {    // bogus, just to show logging                 throw new IllegalArgumentException("Just testing");             }   "I created an object: " + o);         } catch (Throwable t) {             LogRecord msg = new LogRecord(Level.SEVERE,                 "Caught exception ");             msg.setThrown(t);             myLogger.log(msg);         }     } }

Java Cookbook
Java Cookbook, Second Edition
ISBN: 0596007019
EAN: 2147483647
Year: 2003
Pages: 409
Authors: Ian F Darwin

Similar book on Amazon © 2008-2017.
If you may any questions please contact us: