![]() |
CS 635: Advanced Object-Oriented Design & Programming |
|---|
| References | slide # 1 |
| Logging System | slide # 2 |
| ...Registering a Logger | slide # 4 |
| Java and the Singleton | slide # 7 |
Unloading Java Classes That Contain Static Fields, McDowell & Baldwin, ACM SIGPLAN, v 33 n1, January 1998, pp 56-60
The first, Diagnostic logger uses the singleton
Logging of each type can be turn off, without having to remove the logging statements from code

private static boolean hasBeenSet = false;
private static LoggerImplementation activeLogger =
new ScreenLogger();
public static void register( LoggerImplementation aLogger)
{
if ( ! hasBeenSet )
{
activeLogger = aLogger;
preventClassGarbageCollection();
}
}
In File Loggerpublic static FileLogger register( String fileName )
throws LoggerCreationException
{
FileLogger instance =
new FileLogger( fileName, true );
Logger.register( instance );
return instance;
}
Using Logger Classesimport 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" );
}
}
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
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
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