|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectcom.ccg.util.Log
public class Log
Class to facilitate a general purpose message logging system.
In our C++ libraries we had a handy message logging mechanism which
enabled us to easily log messages when error, warning, information,
and other types of conditions occurred. This class is designed to
implement the same type of environment, but also enable the
localization of the log messages. Please refer to the LogDebug example program (and its
source
code) for a decent working example of using this class with
localization values.
NOTE:This class is really intended for non-debugging
output. The Debug class has been provided (which makes use
of this class) for logging debugging type of information.
UNFORTUNATELY, after thorough use of this class, it was
discovered that its method to localize messages to be
inadequate. To realistically use this class for localization
purposes, a Log object should be created with its own
resource bundle. The static methods should not be doing any
of the localization themselves. Unfortunately to change this would
have ramifications for a lot of existing code. New users of the
Log() class should use the following methods for logging
messages (and localize the messages themselves):
Log.always("Message");
Log.info("Message");
Log.error("Message");
Log.error("Message",exception);
Log.warning("Message");
Debug.isEnabledFor(level)) Debug.out("Message");
Debug.log(level,"Message"); // for simple messages
Debug,
log(int,String,String,Object[])| Field Summary | |
|---|---|
static int |
ALWAYS
Log level for messages which should ALWAYS be logged. |
static int |
DEBUG
Log level for messages which provide debugging information. |
static int |
ERROR
Log level for messages which indicate an error condition. |
static int |
INFO
Log level for messages which provide information to the user. |
static int |
WARNING
Log level for messages which indicate an warning condition. |
| Constructor Summary | |
|---|---|
Log()
|
|
| Method Summary | |
|---|---|
static void |
addLogViewer(Observer o)
Add an additional log viewer object. |
static void |
always(String msg)
Wrapper function to log a message at the ALWAYS level. |
static void |
always(String key,
String dMsg,
Object[] args)
Wrapper function to log a message at the ALWAYS level. |
static void |
error(String msg)
Wrapper function to log a message at the ERROR level. |
static void |
error(String key,
String dMsg,
Object[] args)
Wrapper function to log a message at the ERROR level. |
static void |
error(String msg,
Throwable e)
Wrapper function to log a message at the ERROR level. |
static String |
format(int ll,
int dl)
Format a log level (and possibly a debug verbosity) as a string. |
static int |
getLevel()
Examine the current level of output. |
static String[] |
getLogLevelStrings()
Get all of the possible (recognized) log level settings (in their string representation). |
static int[] |
getLogLevelValues()
Get all of the possible (recognized) log level settings (in their integer form). |
static void |
info(String msg)
Wrapper function to log a message at the INFO level. |
static void |
info(String key,
String dMsg,
Object[] args)
Wrapper function to log a message at the INFO level. |
static void |
init(String basename,
Lookup cl)
Initialize the properties of logging based on command line options. |
static void |
init(String basename,
Lookup l,
Observer o)
Initialize the properties of logging based on command line options. |
static void |
log(int lev,
String key,
String dMsg,
Object[] args)
Log a message. |
static String |
logLevelToString(int ll,
String badVal)
Convert a log level to a string representation. |
static void |
removeLogViewer(Observer o)
Remove a single log viewer object. |
static void |
setLevel(int new_level)
Set the output log level. |
static void |
setLogViewer(Observer o)
Set the log viewer object. |
static void |
setResourceBundle(ResourceBundle bundle)
Set the resource bundle to use for language translations. |
int |
stringToLogLevel(String llstr,
int badVal)
Convert a string value to its corresponding log level. |
static boolean |
validateDebugLevel(int ll)
Verify a debug verbosity level. |
static boolean |
validateLogLevel(int ll)
Verify a log level value is valid. |
static void |
warning(String msg)
Wrapper function to log a message at the WARNING level. |
static void |
warning(String key,
String dMsg,
Object[] args)
Wrapper function to log a message at the WARNING level. |
| Methods inherited from class java.lang.Object |
|---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
| Field Detail |
|---|
public static final int ALWAYS
setLevel(int),
log(int,String,String,Object[]),
Constant Field Valuespublic static final int ERROR
setLevel(int),
log(int,String,String,Object[]),
Constant Field Valuespublic static final int INFO
setLevel(int),
log(int,String,String,Object[]),
Constant Field Valuespublic static final int WARNING
setLevel(int),
log(int,String,String,Object[]),
Constant Field Valuespublic static final int DEBUG
Debug.log() function
for debug output instead.
setLevel(int),
Debug.isEnabledFor(int),
Debug.out(java.lang.String, java.lang.String, java.lang.Object[]),
log(int,String,String,Object[]),
Constant Field Values| Constructor Detail |
|---|
public Log()
| Method Detail |
|---|
public static void always(String key,
String dMsg,
Object[] args)
ALWAYS level.
This function provides a wrapper around the log() function. It is intended
to save typing and enables you to do the following:
Log.always("key","Default output message",null);
Instead of the following:
Log.log(Log.ALWAYS,"key","Default output message",null);
key - Used to look up any locale specific string. If null or the
locale specific string is not found, then dMsg will be used
for the string.dMsg - Default string message (if key is null or locale specific
message isn't found).args - If non-null, up to 10 java.text.MessageFormat parameters that
can be subsituted into the message.log(int,String,String,Object[])public static void always(String msg)
ALWAYS level.
This function provides a wrapper around the log() function. It is intended to save typing
and enables you to do something like the following:
String msg = myResourceBundle.getString("ALWAYS.210");
Log.always(msg);
It should be noted that this version does not attempt any localization of the message for you (you will need to do that yourself).
msg - Message to log - it should already be localized for the locale.log(int, java.lang.String, java.lang.String, java.lang.Object[])
public static void error(String key,
String dMsg,
Object[] args)
ERROR level.
This function provides a wrapper around the log() function. It is intended
to save typing and enables you to do the following:
Log.error("key","Default output message",null);
Instead of the following:
Log.log(Log.ERROR,"key","Default output message",null);
key - Used to look up any locale specific string. If null or the
locale specific string is not found, then dMsg will be used
for the string.dMsg - Default string message (if key is null or locale specific
message isn't found).args - If non-null, up to 10 java.text.MessageFormat parameters that
can be subsituted into the message.log(int,String,String,Object[])public static void error(String msg)
ERROR level.
This function provides a wrapper around the log() function. It is intended to save typing
and enables you to do something like the following:
String msg = myResourceBundle.getString("ERR.101");
Log.error(msg);
It should be noted that this version does not attempt any localization of the message for you (you will need to do that yourself).
msg - Message to log - it should already be localized for the locale.error(String,Throwable),
log(int, java.lang.String, java.lang.String, java.lang.Object[])
public static void error(String msg,
Throwable e)
ERROR level.
This function provides a wrapper around the log() function. It is intended to save typing
and enables you to do something like the following:
String msg = myResourceBundle.getString("ERR.101");
Log.error(msg);
It should be noted that this version does not attempt any localization of the message for you (you will need to do that yourself).
msg - Message to log - it should already be localized for the locale
- it is OK to pass null.e - Any Throwable object (which all java.lang.Exceptions are). If
non-null, the stack trace will be appended to the 'msg'.error(String,Throwable),
log(int, java.lang.String, java.lang.String, java.lang.Object[])
public static void info(String key,
String dMsg,
Object[] args)
INFO level.
This function provides a wrapper around the log()
function. It is intended to save typing and enables you to do the
following:
Log.info("key","Default output message",null);
Instead of the following:
Log.log(Log.INFO,"key","Default output message",null);
key - Used to look up any locale specific string. If null or the
locale specific string is not found, then dMsg will be used
for the string.dMsg - Default string message (if key is null or locale specific
message isn't found).args - If non-null, up to 10 java.text.MessageFormat parameters that
can be subsituted into the message.log(int,String,String,Object[])public static void info(String msg)
INFO level.
This function provides a wrapper around the log() function. It is intended to save typing
and enables you to do something like the following:
String msg = myResourceBundle.getString("INFO.014");
Log.info(msg);
It should be noted that this version does not attempt any localization of the message for you (you will need to do that yourself).
msg - Message to log - it should already be localized for the locale.log(int, java.lang.String, java.lang.String, java.lang.Object[])
public static void warning(String key,
String dMsg,
Object[] args)
WARNING level.
This function provides a wrapper around the log()
function. It is intended to save typing and enables you to do the
following:
Log.warning("key","Default output message",null);
Instead of the following:
Log.log(Log.WARNING,"key","Default output message",null);
key - Used to look up any locale specific string. If null or the
locale specific string is not found, then dMsg will be used
for the string.dMsg - Default string message (if key is null or locale specific
message isn't found).args - If non-null, up to 10 java.text.MessageFormat parameters that
can be subsituted into the message.log(int,String,String,Object[])public static void warning(String msg)
WARNING level.
This function provides a wrapper around the log() function. It is intended to save typing
and enables you to do something like the following:
String msg = myResourceBundle.getString("WARN.012");
Log.warning(msg);
It should be noted that this version does not attempt any localization of the message for you (you will need to do that yourself).
msg - Message to log - it should already be localized for the locale.log(int, java.lang.String, java.lang.String, java.lang.Object[])public static void setResourceBundle(ResourceBundle bundle)
log(int,String,String,Object[]) function when it
performs language translations. This will enable future "log"
messages the ability to be translated to the proper locale.
bundle - New resource bundle to use for language translations.log(int,String,String,Object[])public static void setLevel(int new_level)
int - New log level to use.log(int,String,String,Object[])
public static void init(String basename,
Lookup cl)
If a resource bundle hasn't been loaded yet for the logger
(via setResourceBundle), then an
attempt will be made to load the resource bundle associated with
the application for the current locale.
This routine will look for a set of common command line options and use those options to setup the output logging environment. The current command line options which are looked for include:
DEBUG level for output. In addition,
if the optional debug LEVEL (as a number from 0-9) is specified,
it will also set the debug verbosity level to the level specified
or the default value of 3 if not specified.
setLevel(int). The LEVEL must be one of the following
"always", "error", "info", "warning", or "debug". This option is
only checked if -debug [LEVEL] is not set.
In the future options will probably be added to allow for redirection of the output log messages.
basename - Base name of application used when trying to fetch the
appropriate resource bundle for the logger.cl - Command line arguments.log(int,String,String,Object[])
public static void init(String basename,
Lookup l,
Observer o)
If a resource bundle hasn't been loaded yet for the logger
(via setResourceBundle), then an
attempt will be made to load the resource bundle associated with
the application for the current locale.
This routine will look for a set of common command line options and use those options to setup the output logging environment. The current command line options which are looked for include:
DEBUG level for output. In addition,
if the optional debug LEVEL (as a number from 0-9) is specified,
it will also set the debug verbosity level to the level specified
or the default value of 3 if not specified.
setLevel(int). The LEVEL must be one of the following
"always", "error", "info", "warning", or "debug". This option is
only checked if -debug [LEVEL] is not set.
In the future options will probably be added to allow for redirection of the output log messages.
basename - Base name of application used when trying to fetch the
appropriate resource bundle for the logger.cl - Command line arguments (or null if none).logger - The actual object used to record the text log. By default (if
you pass null), a object will be created that puts the log
contents to System.err.log(int,String,String,Object[])public static void setLogViewer(Observer o)
class MyLogViewer implements Observable {
public void update(Observable o, Object logThis) {
if (logThis != null) System.out.println(logThis.toString());
}
// Install as only log viewer when created
MyLogViewer() { Log.setLogViewer(this); }
}
arg1 - New observer that will monitor (and possibly record) the log
messages that pass through the logger. NOTE: It is OK to pass
null, it will effectively turn off ALL logging.init(String,Lookup,Observer),
addLogViewer(java.util.Observer)public static void addLogViewer(Observer o)
This routine is particularily useful for those circumstances
when you want to "peek" at the log messages which are
occurring. You can use this method to look at the messages for
awhile with your handler and then use removeLogViewer(java.util.Observer)
to remove them when you are done - all without affecting any of
the other registered log viewers.
The handlers are implemented in a standard
java.lang.Observable form. Once you've added a viewer, you can
later remove it if desired by using removeLogViewer(java.util.Observer). The
following demonstrates how one might construct their own custom
handler:
class MyLogViewer implements Observable {
public void update(Observable o, Object logThis) {
if (logThis != null) System.out.println(logThis.toString());
}
public void remove() { // allow the viewer to be removed
Log.removeLogViewer(this);
}
// Install as additional
// log viewer when created
MyLogViewer() { Log.addLogViewer(this); }
}
arg1 - New observer that will also monitor (and possibly record) the
log messages that pass through the logger. NOTE: passing null
is OK (but it doesn't affect anything).init(String,Lookup,Observer),
setLogViewer(java.util.Observer),
removeLogViewer(java.util.Observer)public static void removeLogViewer(Observer o)
addLogViewer(java.util.Observer).
arg1 - Observer object to remove (that had been previously
added). NOTE: It is OK to pass null, it just doesn't do anything.addLogViewer(java.util.Observer)
public static void log(int lev,
String key,
String dMsg,
Object[] args)
setLevel(int) member.
Typically you will not invoke this function directly, you would instead use one of the wrapper functions:
Log.always() - to log very important information.
Log.error() - to log severe error messages.
Log.info() - to log information messages.
Log.warning() - to log warning type messages.
Debug.log - to
log debug output messages.
NOTE: Do NOT use this function directly for debug output. Use
the Debug class instead. It will indirectly use this
class, but has the option to optimize out a lot of code when you
don't need a debug build.
vlev - Verbosity level. This should be in the range of [0-9]. Output
will only be logged if this level is less than the current
verbosity level. This was done to mimic a lot of GNU code.key - Used to look up any locale specific string. If null or the
locale specific string is not found, then dMsg will be used
for the string.dMsg - Default string message (if key is null or locale specific
message isn't found).args - If non-null, up to 10 java.text.MessageFormat parameters that
can be subsituted into the message.setLevel(int),
always(java.lang.String, java.lang.String, java.lang.Object[]),
error(java.lang.String, java.lang.String, java.lang.Object[]),
info(java.lang.String, java.lang.String, java.lang.Object[]),
warning(java.lang.String, java.lang.String, java.lang.Object[]),
Debug.log(int,String,String,Object[])public static int getLevel()
setLevel(int)setLevel(int)public static boolean validateLogLevel(int ll)
ll - A "log level" that you would like to have verified.
public static boolean validateDebugLevel(int ll)
ll - A "debug level" that you would like to have verified.
public static String[] getLogLevelStrings()
getLogLevelValues().getLogLevelValues()public static int[] getLogLevelValues()
getLogLevelStrings().getLogLevelStrings()
public static String logLevelToString(int ll,
String badVal)
ll - The log level to convert to string representation - should be
one of the standard log level constants defined (like: Log.ERROR.badVal - What to return if the log level passed is not valid.
format(int, int)
public int stringToLogLevel(String llstr,
int badVal)
llstr - The log level as a string (like "error") to convert to a log
level constant (note we are case insensitive). However, you
must not pass null.badVal - The value to return (typically one of the standard log mode
constants - or -1) if the string could not be converted.
format(int, int)
public static String format(int ll,
int dl)
This method takes a log level setting and a debug verbosity
value and produces a single string representation. The string
representation produced will only include the debug verbosity if
the log level passed is Log.DEBUG.
For example, you might get strings like: "error", "debug 3", "warning" out of this method.
ll - The log level.dl - Debug level verbosity (only used if the log level is Log.DEBUG.
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||