|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectcern.cmw.Logger
public abstract class Logger
Defines a general API for message logging and allows to connect it
to a system specific logging facility.
An application can create an arbitrary number of named Logger objects,
each logger can be used to log message for an application component or
a specific activity in the application. Logger objects may be obtained
by calls to the getLogger(java.lang.String)
factory method.
//
// Get a logger for the "poller" subsystem
//
Logger pollerLog = Logger.getLogger("poller");
//
// Get a logger to trace timing events
//
Logger timingLog = Logger.getLogger("timing");
A logger can be used to log text messages at different
levels
.
On each logging call the logger performs a check of the message level
against a log level maintained by the logger. If the message level is
higher than the log level, the logging call returns immediately, otherwise
the message will be forwarded to the log output.
//
// Enable ERROR, WARNING and INFO messages
//
pollerLog.setLevel(Logger.LOG_INFO);
//
// This message will be not logged
//
pollerLog.debug("Entering Poller.init()");
//
// This message will be logged
//
pollerLog.info("Initializing poller");
//
// This message will be not logged
//
pollerLog.trace("Connecting to timing");
//
// This message will be logged
//
pollerLog.warning("Event source undefined, using default");
//
// This message will be logged
//
pollerLog.error("Can't connect to timing, exiting");
See also the main(java.lang.String[])
function of this class for an example of how to use
loggers.
By default, Logger sends messages to the standard output. This default
behaviour can be modified, and the messages can be forwarded to a system
specific logging mechanism: see init(java.lang.String)
and LoggerFactory
for more details.
Constructor Summary | |
---|---|
protected |
Logger(String name)
Constructor. |
Method Summary | |
---|---|
void |
debug(String message)
Logs a DEBUG message. |
static void |
destroy()
Calling this method will safely close the logging system and release all related resources. |
void |
error(String message)
Logs an ERROR message. |
static LogLevel |
getDefaultLevel()
Returns the default log level. |
LogLevel |
getLevel()
Returns the log level maintained by this logger. |
static Logger |
getLogger(String name)
Returns a logger identified by the name. |
static Enumeration |
getLoggers()
Returns an enumeration of known loggers. |
static String |
getLoggingType()
Returns a string identifier of the underlying logging mechanism, e.g. the string "default" for default logging. |
String |
getName()
Returns the name of this logger. |
void |
info(String message)
Logs an INFO message. |
static void |
init()
Initializes the logging API and connects it to a system specific logging mechanism. |
static void |
init(String factory)
Initializes the logging API and connects it to a system specific logging mechanism. |
static boolean |
initialized()
Returns true if the logging system has been initialized. |
boolean |
isLoggable(LogLevel level)
Returns true if a message of the given level would actually be logged by this logger. |
protected abstract void |
log(LogLevel level,
String message)
Forwards the message to the log output. |
static void |
main(String[] args)
For tests. |
static void |
setDefaultLevel(LogLevel level)
Sets the default log level to the specified level. |
void |
setLevel(LogLevel level)
Sets the log level maintained by this logger. |
static void |
setLevels(LogLevel level)
Sets the specified level in all loggers in the system. |
void |
trace(String message)
Logs a TRACE message. |
void |
warning(String message)
Logs a WARNING message. |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait |
Constructor Detail |
---|
protected Logger(String name)
name
- the logger nameMethod Detail |
---|
public static void init(String factory)
factory
- the fully qualified class name of the factory object
CmwInternalError
- thrown if the factory class cannot be loaded, or the class
does not implement the LoggerFactory interface.LoggerFactory
public static void init()
cmw.loggerFactory=cmw.logging.L4JLoggerFactory
If this property is not defined then the default logging
will be used. This method does nothing if the logging system has
already been initialized.
CmwInternalError
- thrown if the factory class specified by the property cannot be
loaded, or the class does not implement the LoggerFactory interface.LoggerFactory
public static boolean initialized()
public static void destroy()
public static Logger getLogger(String name)
LogLevel.OFF
.
The default level can be checked and changed at run time by calling
the getDefaultLevel()
and setDefaultLevel(cern.cmw.LogLevel)
methods.
CmwInternalError
- thrown if the logging system is not initialized.public static LogLevel getDefaultLevel()
getLogger
public static void setDefaultLevel(LogLevel level)
getLogger
public LogLevel getLevel()
public void setLevel(LogLevel level)
public static void setLevels(LogLevel level)
public boolean isLoggable(LogLevel level)
public String getName()
public void error(String message)
LogLevel.ERROR
level
then the specified message is forwarded to the log output.
public void warning(String message)
LogLevel.WARNING
level
then the specified message is forwarded to the log output.
public void info(String message)
LogLevel.INFO
level
then the specified message is forwarded to the log output.
public void trace(String message)
LogLevel.TRACE
level
then the specified message is forwarded to the log output.
public void debug(String message)
LogLevel.DEBUG
level
then the specified message is forwarded to the log output.
protected abstract void log(LogLevel level, String message)
public static Enumeration getLoggers()
public static String getLoggingType()
public static void main(String[] args)
public static void main(String[] args)
{
try
{
Config.load();
Logger.init();
}
catch(Exception ex)
{
System.out.println(ex);
System.exit(0);
}
System.out.println("Using '"+Logger.getLoggingType()+"' logging");
Logger test = Logger.getLogger("test");
LogLevel[] levels =
{
LogLevel.ALL,
LogLevel.DEBUG,
LogLevel.TRACE,
LogLevel.INFO,
LogLevel.WARNING,
LogLevel.ERROR,
LogLevel.OFF
};
for (int i=0; i
The output with the default Logger is:
Using 'default' logging Testing for ALL ERROR - CMW Logger test, log level: ALL WARNING - CMW Logger test, log level: ALL INFO - CMW Logger test, log level: ALL TRACE - CMW Logger test, log level: ALL DEBUG - CMW Logger test, log level: ALL Testing for DEBUG ERROR - CMW Logger test, log level: DEBUG WARNING - CMW Logger test, log level: DEBUG INFO - CMW Logger test, log level: DEBUG TRACE - CMW Logger test, log level: DEBUG DEBUG - CMW Logger test, log level: DEBUG Testing for TRACE ERROR - CMW Logger test, log level: TRACE WARNING - CMW Logger test, log level: TRACE INFO - CMW Logger test, log level: TRACE TRACE - CMW Logger test, log level: TRACE Testing for INFO ERROR - CMW Logger test, log level: INFO WARNING - CMW Logger test, log level: INFO INFO - CMW Logger test, log level: INFO Testing for WARNING ERROR - CMW Logger test, log level: WARNING WARNING - CMW Logger test, log level: WARNING Testing for ERROR ERROR - CMW Logger test, log level: ERROR Testing for OFF
|
||||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |