public class Logger
extends java.lang.Object
Logger wraps the
Jakarta Commons Logging
API and provides a slightly simpler, but similar, interface. This
class supports most of the logging methods, but it doesn't actually
instantiate an underlying java.util.logging.Logger object until
(or unless) a thread explicitly calls the enableLogging()
method.
The first call to enableLogging() traverses the list of
instantiated org.clapper.util.Logger objects and creates
underlying java.util.logging.Logger objects for each one. Any
Logger objects created after enableLogging() is called
are automatically enabled.
This approach prevents any interaction with the real logging layer, unless logging is explicitly enabled (e.g., because a command-line option has been specified). This approach was originally taken to avoid annoying startup messages from the Log4J logging implementation, which insists on writing warning messages to the console when it can't find a configuration file.
However, this class can be reimplemented in terms of other logging layers (and, in fact, has been implemented solely in terms of the JDK 1.4 native logging library in the past). This object's main purpose now is to insulate applications from the underlying logging technology, so that technology can be changed, if necessary, without having an impact on applications that use this class.
The level mappings used by this class are identical to those used by Commons Logging. (e.g.,, a "debug" message uses the same level as a Commons Logging "debug" message.) Those mappings are:
org.clapper.util.logging.Logger method | Corresponding java.util.logging.Level value |
---|---|
debug() | FINE |
error() | SEVERE |
fatal() | SEVERE |
info() | INFO |
trace() | FINEST |
warn() | WARNING |
If you prefer to use Commons Logging directly, then, by all means, use it for your applications.
WARNING: If your application installs its own class loader (e.g., as the thread context class loader), you may have problems with both Jakarta Commons Logging and java.util.logging (which Jakarta Commons Logging uses as its default logging implementation).
java -Dorg.apache.commons.logging.Log=org.apache.commons.logging.impl.Log4JLogger -Dlog4j.configuration=file:/path/to/log4j-debug.properties
enableLogging()
,
Jakarta Commons Logging APIModifier and Type | Field and Description |
---|---|
static LogLevel |
LEVEL_DEBUG
Log level constant for debug messages.
|
static LogLevel |
LEVEL_ERROR
Log level constant for error messages.
|
static LogLevel |
LEVEL_FATAL
Log level constant for fatal-error messages.
|
static LogLevel |
LEVEL_INFO
Log level constant for informational messages.
|
static LogLevel |
LEVEL_TRACE
Log level constant for trace messages.
|
static LogLevel |
LEVEL_WARNING
Log level constant for warning messages.
|
Constructor and Description |
---|
Logger(java.lang.Class cls)
Construct a new Logger object
|
Logger(java.lang.String className)
Construct a new Logger object
|
Modifier and Type | Method and Description |
---|---|
void |
debug(java.lang.Object message)
Log a message with debug log level.
|
void |
debug(java.lang.Object message,
java.lang.Throwable ex)
Log an error with debug log level.
|
static void |
enableLogging()
Enable logging by instantiating the underlying Log objects.
|
void |
error(java.lang.Object message)
Log a message with error log level.
|
void |
error(java.lang.Object message,
java.lang.Throwable ex)
Log an error with error log level.
|
void |
fatal(java.lang.Object message)
Log a message with fatal log level.
|
void |
fatal(java.lang.Object message,
java.lang.Throwable ex)
Log an error with fatal log level.
|
void |
info(java.lang.Object message)
Log a message with info log level.
|
void |
info(java.lang.Object message,
java.lang.Throwable ex)
Log an error with info log level.
|
boolean |
isDebugEnabled()
Determine whether debug logging is currently enabled.
|
boolean |
isErrorEnabled()
Determine whether error logging is currently enabled.
|
boolean |
isFatalEnabled()
Determine whether fatal logging is currently enabled.
|
boolean |
isInfoEnabled()
Determine whether info logging is currently enabled.
|
boolean |
isTraceEnabled()
Determine whether trace logging is currently enabled.
|
boolean |
isWarningEnabled()
Determine whether warn logging is currently enabled.
|
void |
message(LogLevel level,
java.lang.Object message)
Log a message at a specified log level.
|
void |
message(LogLevel level,
java.lang.Object message,
java.lang.Throwable ex)
Log a message at a specified log level.
|
void |
trace(java.lang.Object message)
Log a message with trace log level.
|
void |
trace(java.lang.Object message,
java.lang.Throwable ex)
Log an error with trace log level.
|
void |
warn(java.lang.Object message)
Log a message with warn log level.
|
void |
warn(java.lang.Object message,
java.lang.Throwable ex)
Log an error with warn log level.
|
public static LogLevel LEVEL_DEBUG
LogLevel.DEBUG
public static LogLevel LEVEL_ERROR
LogLevel.ERROR
public static LogLevel LEVEL_FATAL
LogLevel.FATAL
public static LogLevel LEVEL_INFO
LogLevel.INFO
public static LogLevel LEVEL_TRACE
LogLevel.TRACE
public static LogLevel LEVEL_WARNING
LogLevel.WARNING
public Logger(java.lang.String className)
className
- the class name to associate with the objectpublic Logger(java.lang.Class cls)
cls
- the class whose name should be associated with the objectpublic static void enableLogging() throws java.lang.UnsupportedOperationException
java.lang.UnsupportedOperationException
- could not find Commons Logging
APIpublic void debug(java.lang.Object message)
message
- The message to convert to a string and logpublic void debug(java.lang.Object message, java.lang.Throwable ex)
message
- The message to convert to a string and logex
- The exception to log with the messagepublic void error(java.lang.Object message)
message
- The message to convert to a string and logpublic void error(java.lang.Object message, java.lang.Throwable ex)
message
- The message to convert to a string and logex
- The exception to log with the messagepublic void fatal(java.lang.Object message)
message
- The message to convert to a string and logpublic void fatal(java.lang.Object message, java.lang.Throwable ex)
message
- The message to convert to a string and logex
- The exception to log with the messagepublic void info(java.lang.Object message)
message
- The message to convert to a string and logpublic void info(java.lang.Object message, java.lang.Throwable ex)
message
- The message to convert to a string and logex
- The exception to log with the messagepublic void message(LogLevel level, java.lang.Object message)
level
- the log levelmessage
- the messagepublic void message(LogLevel level, java.lang.Object message, java.lang.Throwable ex)
level
- the log levelmessage
- the messageex
- The exception to log with the messagepublic void trace(java.lang.Object message)
message
- The message to convert to a string and logpublic void trace(java.lang.Object message, java.lang.Throwable ex)
message
- The message to convert to a string and logex
- The exception to log with the messagepublic void warn(java.lang.Object message)
message
- The message to convert to a string and logpublic void warn(java.lang.Object message, java.lang.Throwable ex)
message
- The message to convert to a string and logex
- The exception to log with the messagepublic boolean isDebugEnabled()
public boolean isErrorEnabled()
public boolean isFatalEnabled()
public boolean isInfoEnabled()
public boolean isTraceEnabled()
public boolean isWarningEnabled()