Using a java.util.logging FileHandler

With a FileHandler, you can log to one or more files. Here is a simple example that writes to one log file in the current directory. The default formatter for FileHandler is XMLFormatter, so every log entry is an XML segment.

Main.java:

import java.util.logging.*;
import java.io.*;
 
public class Main
{
   public static void main(String argv[]) throws IOException {
      Logger logger = Logger.getLogger("main");
      logger.setUseParentHandlers(false);
      logger.setLevel(Level.ALL);
 
      FileHandler fh = new FileHandler("Main.log");
 
      fh.setLevel(Level.ALL);
      logger.addHandler(fh);
 
      for (int i=0; i<1000; i++) {
         logger.severe("log message #1");
         logger.warning("log message #2");
         logger.info("log message #3");
         logger.config("log message #4");
         logger.fine("log message #5");
      }
   }
}

You can specify the maximum size of a log file along with a pattern that will be used to dynamically determine the name of the log file.

For example:

Main.java:

import java.util.logging.*;
import java.io.*;
 
public class Main
{
   public static void main(String argv[]) throws IOException {
      Logger logger = Logger.getLogger("main");
      logger.setUseParentHandlers(false);
      logger.setLevel(Level.ALL);
 
      FileHandler fh = new FileHandler("%t/logfiles/Main.%g.log", 10000, 10);
 
      fh.setLevel(Level.ALL);
      logger.addHandler(fh);
 
      for (int i=0; i<1000; i++) {
         logger.severe("log message #1");
         logger.warning("log message #2");
         logger.info("log message #3");
         logger.config("log message #4");
         logger.fine("log message #5");
      }
   }
}

The pattern %t/logfiles/Main.%g.log tells the FileHandler to write to the subdirectory logfiles in the temporary directory (%t). It will use a maximum of 10 files with a limit of approx 10K. To determine the the temporary directory on your system, FileHandler will look at the property java.io.tmpdir. (Find out with System.out.println(System.getProperty(“java.io.tmpdir”));)

This is the directory listing on my system after running the example:

C:Documents and SettingsAdministratorLocal SettingsTemplogfiles>dir
 Volume in drive C has no label.
 Volume Serial Number is E4D8-7443

 Directory of C:Documents and SettingsAdministratorLocal SettingsTemplogfiles

01/20/2002  03:32p      <DIR>          .
01/20/2002  03:32p      <DIR>          ..
01/20/2002  03:32p               5,970 Main.0.log
01/20/2002  03:32p                   0 Main.0.log.lck
01/20/2002  03:32p              10,234 Main.1.log
01/20/2002  03:32p              10,231 Main.2.log
01/20/2002  03:32p              10,234 Main.3.log
01/20/2002  03:32p              10,234 Main.4.log
01/20/2002  03:32p              10,233 Main.5.log
01/20/2002  03:32p              10,234 Main.6.log
01/20/2002  03:32p              10,231 Main.7.log
01/20/2002  03:32p              10,234 Main.8.log
01/20/2002  03:32p              10,234 Main.9.log
              11 File(s)         98,069 bytes
               2 Dir(s)   6,371,955,200 bytes free

For more information about the patterns that can be used, check here:
http://java.sun.com/j2se/1.4/docs/api/java/util/logging/FileHandler.html