| 1 |
ajm |
1.1 |
//---PACKAGE DECLARATION--- |
| 2 |
|
|
//package uk.org.iscream.core; |
| 3 |
|
|
|
| 4 |
|
|
//---IMPORTS--- |
| 5 |
|
|
//import uk.org.iscream.util.*; |
| 6 |
|
|
|
| 7 |
|
|
/** |
| 8 |
|
|
* Allows classes to send logging information |
| 9 |
|
|
* to an implementation of the LoggerImpl interface. |
| 10 |
|
|
* |
| 11 |
|
|
* @author $Author: tdb1 $ |
| 12 |
|
|
* @version $Id: LoggerServant.java,v 1.9 2001/03/14 23:25:29 tdb1 Exp $ |
| 13 |
|
|
*/ |
| 14 |
|
|
class Logger { |
| 15 |
|
|
|
| 16 |
|
|
//---FINAL ATTRIBUTES--- |
| 17 |
|
|
|
| 18 |
|
|
/** |
| 19 |
|
|
* The current CVS revision of this class |
| 20 |
|
|
*/ |
| 21 |
|
|
public final String REVISION = "$Revision: 1.9 $"; |
| 22 |
|
|
|
| 23 |
|
|
/** |
| 24 |
|
|
* The verbosity value for the FATAL log message |
| 25 |
|
|
*/ |
| 26 |
|
|
public static final int FATAL = 0; |
| 27 |
|
|
|
| 28 |
|
|
/** |
| 29 |
|
|
* The verbosity value for the ERROR log message |
| 30 |
|
|
*/ |
| 31 |
|
|
public static final int ERROR = 1; |
| 32 |
|
|
|
| 33 |
|
|
/** |
| 34 |
|
|
* The verbosity value for the WARNING log message |
| 35 |
|
|
*/ |
| 36 |
|
|
public static final int WARNING = 2; |
| 37 |
|
|
|
| 38 |
|
|
/** |
| 39 |
|
|
* The verbosity value for the SYSMSG log message |
| 40 |
|
|
*/ |
| 41 |
|
|
public static final int SYSMSG = 3; |
| 42 |
|
|
|
| 43 |
|
|
/** |
| 44 |
|
|
* The verbosity value for the SYSINIT log message |
| 45 |
|
|
*/ |
| 46 |
|
|
public static final int SYSINIT = 4; |
| 47 |
|
|
|
| 48 |
|
|
/** |
| 49 |
|
|
* The verbosity value for the DEBUG log message |
| 50 |
|
|
*/ |
| 51 |
|
|
public static final int DEBUG = 5; |
| 52 |
|
|
|
| 53 |
|
|
/** |
| 54 |
|
|
* An array of names of verbosity levels. |
| 55 |
|
|
* Thus logging messages are now "classed" by the level" |
| 56 |
|
|
* This string is prepended to the "source" of a logging message. |
| 57 |
|
|
*/ |
| 58 |
|
|
public final static String[] VERBOSITY_NAMES = {"FATAL", "ERROR", "WARNING", "SYSMSG", "SYSINIT", "DEBUG"}; |
| 59 |
|
|
|
| 60 |
|
|
//---STATIC METHODS--- |
| 61 |
|
|
|
| 62 |
|
|
/** |
| 63 |
|
|
* Returns a reference to the singleton logger in use |
| 64 |
|
|
* |
| 65 |
|
|
* @throws RuntimeException if not yet initialised |
| 66 |
|
|
*/ |
| 67 |
|
|
public static Logger getInstance() throws RuntimeException { |
| 68 |
|
|
if (_instance == null) { |
| 69 |
|
|
throw new RuntimeException("Cannot obtain instance to uninitialised Logger"); |
| 70 |
|
|
} |
| 71 |
|
|
return _instance; |
| 72 |
|
|
} |
| 73 |
|
|
|
| 74 |
|
|
/** |
| 75 |
|
|
* Constructs and returns the singleton Logger class |
| 76 |
|
|
* |
| 77 |
|
|
* @param logger a reference to the LoggerImpl this will use |
| 78 |
|
|
* @param verbosityLevel the system wide verbosity level in use |
| 79 |
|
|
* |
| 80 |
|
|
* @throws RuntimeException if already initialised |
| 81 |
|
|
*/ |
| 82 |
|
|
public static Logger initialise(LoggerImpl logger, int verbosityLevel) throws RuntimeException { |
| 83 |
|
|
if (_instance != null) { |
| 84 |
|
|
throw new RuntimeException("Already initialised!"); |
| 85 |
|
|
} |
| 86 |
|
|
_instance = new Logger(logger, verbosityLevel); |
| 87 |
|
|
return _instance; |
| 88 |
|
|
} |
| 89 |
|
|
|
| 90 |
|
|
//---CONSTRUCTORS--- |
| 91 |
|
|
|
| 92 |
|
|
/** |
| 93 |
|
|
* Creates a new Logger with a given logging implementation |
| 94 |
|
|
* and a given verbosity level. |
| 95 |
|
|
* This is a private method to ensure singleton pattern. |
| 96 |
|
|
* |
| 97 |
|
|
* @param logger a reference to the LoggerImpl this will use |
| 98 |
|
|
* @param verbosityLevel the system wide verbosity level in use |
| 99 |
|
|
*/ |
| 100 |
|
|
private Logger(LoggerImpl logger, int verbosityLevel) { |
| 101 |
|
|
_logger = logger; |
| 102 |
|
|
_verbosityLevel = verbosityLevel; |
| 103 |
|
|
write(toString(), Logger.SYSINIT, "started"); |
| 104 |
|
|
write(toString(), Logger.SYSMSG, "using verbosity " + _verbosityLevel); |
| 105 |
|
|
} |
| 106 |
|
|
|
| 107 |
|
|
//---PUBLIC METHODS--- |
| 108 |
|
|
|
| 109 |
|
|
/** |
| 110 |
|
|
* The write() method takes a source, verbosity level and |
| 111 |
|
|
* message, and formats them using an external line |
| 112 |
|
|
* formatting method. This line is then given to the logger |
| 113 |
|
|
* to be written. Note that checking of the level is carried |
| 114 |
|
|
* out here. |
| 115 |
|
|
* |
| 116 |
|
|
* @param source A string representation of the calling object. |
| 117 |
|
|
* @param verbosity the verbosity of this message |
| 118 |
|
|
* @param message The text to be logged. |
| 119 |
|
|
*/ |
| 120 |
|
|
public void write(String source, int verbosity, String message) { |
| 121 |
|
|
if (verbosity <= _verbosityLevel) { |
| 122 |
|
|
_logger.write(FormatName.formatLogLine(source, verbosity, message), verbosity); |
| 123 |
|
|
} |
| 124 |
|
|
} |
| 125 |
|
|
|
| 126 |
|
|
/** |
| 127 |
|
|
* Overrides the {@link java.lang.Object#toString() Object.toString()} |
| 128 |
|
|
* method to provide clean logging (every class should have this). |
| 129 |
|
|
* |
| 130 |
|
|
* This uses the uk.org.iscream.util.FormatName class |
| 131 |
|
|
* to format the toString() |
| 132 |
|
|
* |
| 133 |
|
|
* @return the name of this class and its CVS revision |
| 134 |
|
|
*/ |
| 135 |
|
|
public String toString() { |
| 136 |
|
|
return FormatName.getName( |
| 137 |
|
|
_name, |
| 138 |
|
|
getClass().getName(), |
| 139 |
|
|
REVISION); |
| 140 |
|
|
} |
| 141 |
|
|
|
| 142 |
|
|
//---PRIVATE/PROTECTED METHODS--- |
| 143 |
|
|
|
| 144 |
|
|
//---ACCESSOR/MUTATOR METHODS--- |
| 145 |
|
|
|
| 146 |
|
|
//---ATTRIBUTES--- |
| 147 |
|
|
|
| 148 |
|
|
/** |
| 149 |
|
|
* The verbosity level of this instance |
| 150 |
|
|
*/ |
| 151 |
|
|
private int _verbosityLevel; |
| 152 |
|
|
|
| 153 |
|
|
/** |
| 154 |
|
|
* The actual Logger used by this instance |
| 155 |
|
|
*/ |
| 156 |
|
|
private LoggerImpl _logger; |
| 157 |
|
|
|
| 158 |
|
|
/** |
| 159 |
|
|
* This is the friendly identifier of the |
| 160 |
|
|
* component this class is running in. |
| 161 |
|
|
* eg, a Filter may be called "filter1", |
| 162 |
|
|
* If this class does not have an owning |
| 163 |
|
|
* component, a name from the configuration |
| 164 |
|
|
* can be placed here. This name could also |
| 165 |
|
|
* be changed to null for utility classes. |
| 166 |
|
|
*/ |
| 167 |
|
|
private String _name = "Logger"; |
| 168 |
|
|
|
| 169 |
|
|
//---STATIC ATTRIBUTES--- |
| 170 |
|
|
|
| 171 |
|
|
/** |
| 172 |
|
|
* Holds the reference to the singleton instance of his class |
| 173 |
|
|
*/ |
| 174 |
|
|
private static Logger _instance; |
| 175 |
|
|
|
| 176 |
|
|
} |