/**
 * Demonstrate use of Java logging classes
 */
import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;

public class LoggingExample
   {
   /**
    * object to which we direct the log messages.
    */
   Logger logger;

   /**
    * constructor
    */
   public LoggingExample()
      {
      try
         {
         // define where the logging information will go
         // Logs periodically switch files, so you give a pattern, not a precise file name
         FileHandler fh = new FileHandler( "%t/test%g.log"
                                           /* expands to C:/temp/test%g.log pattern for logfiles */,
                                           100000 /* limit chars per file */,
                                           10 /* count of files in cycle before overwrite */,
                                           true /* append to end of existing set of files */);

         // declare how you want your messages timestamped.
         fh.setFormatter( new SimpleFormatter()) ;

         // decide the level of detail to go into this log file.
         fh.setLevel( Level.ALL );

         // get a handle to the object that will do the logging.
         logger = Logger.getLogger("Test Logger");

         // hook up the filehandler to the logger
         logger.addHandler( fh );

         // determine the level of detail the logger will consider.
         logger.setLevel( Level.FINER );

         // debug. make sure FINER messages are truly being logged at this point
         assert logger.isLoggable( Level.FINER ) : "FINER logging not enabled.";

         // log some test messages of various severity.
         logger.log( Level.WARNING,"Water leak. This is a test warning message." );
         logger.finer( "Donut crumbs under Homer's desk. This is test finer message." );
         logger.log( Level.SEVERE, "Nuclear power plant about to go. This is a test severe message." );
         }
      catch ( IOException e )
         {
         e.printStackTrace();
         }
      }

   /**
    * test harness
    *
    * @param args not used
    */
   public static void main ( String[] args )
      {
      new LoggingExample();

      // the log file it creates, C:\temp\test0.log
      // will look something like this
      //  19-Oct-2005 6:14:26 PM LoggingExample <init>
      //  WARNING: Water leak. This is a test warning message.
      //  19-Oct-2005 6:14:26 PM LoggingExample <init>
      //  FINER: Donut crumbs under Homer's desk. This is test finer message.
      //  19-Oct-2005 6:14:26 PM LoggingExample <init>
      //  SEVERE: Nuclear power plant about to go. This is a test severe message.
      //
      // The console will look something like this without the FINER messages:
      //  19-Oct-2005 6:14:26 PM LoggingExample <init>
      //  WARNING: Water leak. This is a test warning message.
      //  19-Oct-2005 6:14:26 PM LoggingExample <init>
      //  SEVERE: Nuclear power plant about to go. This is a test severe message.
      }
   }