SDSU CS 635: Advanced Object-Oriented Design & Programming
Spring Semester, 1998
Logging: A Use of Singleton

To Lecture Notes Index
© 1998, All Rights Reserved, SDSU & Roger Whitney
San Diego State University -- This page last updated 21-Apr-98

Contents of Doc 6, Logging: A Use of Singleton


References slide # 1
Logging System slide # 2
...Registering a Logger slide # 4
Java and the Singleton slide # 7



Doc 6, Logging: A Use of Singleton Slide # 1

References


Patterns for Logging Diagnostic Messages, by Neil Harrison in Pattern Languages of Program Design 3, Eds Martin, Riehle, Buschman, 1998, pp 277-289.

Unloading Java Classes That Contain Static Fields, McDowell & Baldwin, ACM SIGPLAN, v 33 n1, January 1998, pp 56-60



Doc 6, Logging: A Use of Singleton Slide # 2

Logging System


The logging pattern by Harrison uses three patterns

The first, Diagnostic logger uses the singleton

sdsu.logging.Logger

Defines types of logging messages

log
debug
warning
error

You can define your own type of logging message

Logging of each type can be turn off, without having to remove the logging statements from code

Different Loggers

ScreenLogger
Send log data to screen

FileLogger
Send log data to a file

NullLogger
Ignores log data

Doc 6, Logging: A Use of Singleton Slide # 3

Classes

logging classes

Doc 6, Logging: A Use of Singleton Slide # 4

Registering a Logger


In Logger Class

private static boolean hasBeenSet = false;

private static LoggerImplementation activeLogger =
           new ScreenLogger();

public static void register( LoggerImplementation aLogger)
     {
     if ( ! hasBeenSet )
          {
          activeLogger = aLogger;
               
          preventClassGarbageCollection();
          }
     }

In File Logger

Constructor has package access level

public static FileLogger register( String fileName ) 
     throws LoggerCreationException
     {
     FileLogger instance = 
          new FileLogger( fileName, true ); 

     Logger.register( instance );
     return instance;
     }

Doc 6, Logging: A Use of Singleton Slide # 5
Using Logger Classes

import sdsu.logging.*;

public class Tester
     {
     public static void main( String[] args) throws 
          LoggerCreationException
          {
          FileLogger myLog = 
               FileLogger.register( "LogFile");

          myLog.debugOff();
          myLog.warningOff();
          
          // Some where else in the program
          
          Logger.debug( "I am lost, This does not work");
          
          Logger.log( "Routine log stuff" );
          }
     }

Since applications will not normally make consecutive multiple log entries static Logger methods are used to access the singleton rather than using a getInstance method

Doc 6, Logging: A Use of Singleton Slide # 6
Selective Logger
Turning Message Types off/on

To turn off logging of a message type one does the following when one registers the logger:

FileLogger activeLogger = FileLogger.register( "LogFile");
activeLogger.debugOff();
activeLogger.warningOff();

In the rest of the application one still uses static Logger methods to perform logging

The following will have no effect given the above code:

Logger.warning( "This is just a test");

Question

Is there a better way to turn logging off for a message type?

Doc 6, Logging: A Use of Singleton Slide # 7

Java and the Singleton


There is a major problem using the Singleton in java

Classes in java can be garbage collected!

A class can be garbage collected when there are no objects of the class and there are no variable references to the class of the class

If the Logger class is garbage collected then the value of the Singleton will be lost

The next time you reference the Singleton you will get the default value for the Singleton

Solution
Insure that there will be a reference to the class or an instance of the class

Place an instance of the class in a global reference

private static void preventClassGarbageCollection()
     {
     Properties systemTable = System.getProperties();
     systemTable.put( "_$sdsu.logging.Logger.instance",
                                    new Logger());
     }


visitors since 17-Feb-98