Forward Computing and Control Pty. Ltd.
Logging Package V1.3.0

2004/11/22

au.com.forward.logging
Class Logging

java.lang.Object
  extended by au.com.forward.logging.Logging

public class Logging
extends java.lang.Object

This class provides logging initialization and debugging.
Typical use for logging is


  import au.com.forward.logging.*;
  static {
        // set up logging  also redirects System.err and System.out
        // throws a LoggingException if problem with setting up Java logging
        // put all log files in user's home dir
        // roll log to .old when it exceed 100K
        String userHome = System.getProperty("user.home",".")
                         +System.getProperty("file.separator","/");
        Logging.initializeLogging(userHome+"StatusApplication.log",
                "Log File for StatusApplication " + new Date(),true, 100);
        // redirect System.out as well as System.err and append to existing log
   }

If you want your application to continue even if there is an error setting up Java Logging then use the following code
but usually you would want the application to stop if logging fails.
Note: The System.err and System.out redirection are called before initializing Java Logging so they are still in effect even if logging fails.

  static {
       // set up logging  also redirects System.err and System.out
       // throws a LoggingException if problem with setting up Java logging
       // put all log files in user's home dir
        // roll log to .old when it exceed 100K
         String userHome = System.getProperty("user.home",".")
             +System.getProperty("file.separator","/");
       try {
           Logging.initializeLogging(userHome+"StatusApplication.log",
              "Log File for StatusApplication " + new Date(),true, 100);
           // redirect System.out as well as System.err and append to existing log
       } catch (LoggingException lex) {
           System.err.println(RobustFormatter.toString(lex));
       }
   }

If you are using Log4J or some other non-Java logging package, then you should use LogStdStreams.initializeErrorLogging(String, String ,boolean, int) instead to capture the console output from the logger (and any erronous System.out.println's)

Debugging Logging Settings

For debugging your logging settings you can use
   System.out.println(Logging.currentConfiguration());
which will dump the current logging setting in your log file.

Logging Properties File

A sample properties file is

 ############################################################
 #   Logging Configuration File
 #
 # To use this file specify its filename
 # with the java.util.logging.config.file system property.
 #
 # For example:
 #     java -Djava.util.logging.config.file=logging.properties  au.com.forward.ExampleLoggingApplication
 #
 ############################################################
 ############################################################
 #   Global properties
 ############################################################
 # "handlers" specifies a comma separated list of log Handler
 # classes.  These handlers will be installed during VM startup.
 # Note that these classes must be on the system classpath.
 # Add a FileHandler
 handlers=java.util.logging.FileHandler
 # Default global logging level.
 # This specifies which kinds of events are logged across
 # all loggers.  For any given facility this global level
 # can be overridden by a facility specific level
 .level = SEVERE
 ############################################################
 # Handler specific properties.
 # Describes specific configuration info for Handlers.
 ############################################################
 # default file output is in user's home directory.
 java.util.logging.FileHandler.pattern = %h/applicationLog%u.xml
 java.util.logging.FileHandler.formatter = au.com.forward.logging.LoggingXMLFormatter
 ############################################################
 # Facility specific properties.
 # Provides extra control for each logger.
 ############################################################
 # messages:
 au.com.forward.ExampleLoggingApplication.level = FINEST
 

Author:
Matthew Ford

Field Summary
static java.lang.String VERSION
          The version number 1.3
static java.lang.String VERSION_DATE
          The version date 23nd Nov. 2004
 
Method Summary
static java.lang.String currentConfiguration()
          Returns the current logging configuration for debugging.
static void initializeLogging()
          Sets up the default ConsoleHandler without opening a log file
static void initializeLogging(java.lang.String logFileName, java.lang.String initialStr, boolean logStdOut, boolean append)
          Initializes the Log file and sets up the default Console Handler
static void initializeLogging(java.lang.String logFileName, java.lang.String initialStr, boolean logStdOut, int maxSizeInKbytes)
          Initializes a rolling Log file and sets up the default Console Handler
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

VERSION

public static final java.lang.String VERSION
The version number 1.3

See Also:
Constant Field Values

VERSION_DATE

public static final java.lang.String VERSION_DATE
The version date 23nd Nov. 2004

See Also:
Constant Field Values
Method Detail

initializeLogging

public static void initializeLogging(java.lang.String logFileName,
                                     java.lang.String initialStr,
                                     boolean logStdOut,
                                     boolean append)
                              throws LoggingException
Initializes the Log file and sets up the default Console Handler

Parameters:
logFileName - the name of the log file
initialStr - the heading string to write to the log file when opened
logStdOut - true if System.Out to redirected to log file also
append - true if to append to existing log, false to rewrite new log.
Throws:
LoggingException - thrown initializeErrorLogging() has already been called.

initializeLogging

public static void initializeLogging(java.lang.String logFileName,
                                     java.lang.String initialStr,
                                     boolean logStdOut,
                                     int maxSizeInKbytes)
                              throws LoggingException
Initializes a rolling Log file and sets up the default Console Handler

Parameters:
logFileName - the name of the log file
initialStr - the heading string to write to the log file when opened
logStdOut - true if System.Out to redirected to log file also
maxSizeInKbytes - roll current file to .old if it exceeds this size on startup. Any existing .old log file is deleted.
Throws:
LoggingException - thrown initializeErrorLogging() has already been called.

initializeLogging

public static void initializeLogging()
                              throws LoggingException
Sets up the default ConsoleHandler without opening a log file

Throws:
LoggingException - thrown initializeErrorLogging() has already been called.

currentConfiguration

public static java.lang.String currentConfiguration()
Returns the current logging configuration for debugging.

Loggers created in classes are not visable in the current configuration until their class is loaded. This means the best place to call
    System.out.println(Logging.currentConfiguration());
is at the end of the application main() or in the exit code after all the classes have been loaded.

Returns:
a string describing the current logging configuration

Forward Computing and Control Pty. Ltd.
Logging Package V1.3.0

2004/11/22

Copyright ©2003, Forward Computing and Control Pty. Ltd
ACN 003 669 994   NSW Australia,   All Rights Reserved.
email:fcc@forward.com.au