public abstract class LogContext extends Context
This class represents a context for object-based/thread-based logging capabilities.
LogContext removes low level code dependency with the logging framework.
The same code can run using system out/err, standard logging
(java.util.logging
), Log4J or even OSGI Log services.
Selection can be done at run-time through configuration
).
The default
logging context is StandardLog
to leverage java.util.logging
capabilities.
Logging a message is quite simple:(code) LogContext.info("my message");(/code] Because string formatting can be slow, we also find:[code] if (LogContext.isInfoLogged()) LogContext.info("message part 1" + aVar + "message part 2");[/code] Or equivalent but simpler:[code] LogContext.info("message part 1", aVar, "message part 2");[/code]
Logging can be temporarily altered on a thread or object basis. For example:[code] public static main(String[] args) { LogContext.enter(LogContext.NULL); // Temporarily disables logging. try { ClassInitializer.initializeAll(); // Initializes bootstrap, extensions and classpath classes. } finally { LogContext.exit(LogContext.NULL); // Goes back to default logging. } ... }[/code]
Applications may extend this base class to address specific logging requirements. For example:[code] // This class allows for custom logging of session events. public abstract class SessionLog extends LogContext { public static void start(Session session) { LogContext log = LogContext.current(); if (log instanceof SessionLog.Loggable) { ((SessionLog.Loggable)log).logStart(session); } else if (log.infoLogged()){ log.logInfo("Session " + session.id() + " started"); } } public static void end(Session session) { ... } public interface Loggable { void logStart(Session session); void logEnd(Session session); } }[/code]
The use of interfaces (such as Loggable
above) makes it easy
for any context to support customs logging events.
For example:[code]
class MyLog extends StandardLog implements SessionLog.Loggable, DatabaseLog.Loggable {
... // Specialized logging for session and database events.
}
MyLog myLog = new MyLog();
LogContext.enter(myLog);
try {
...
LogContext.info("Informative message"); // Standard logging.
...
DatabaseLog.fail(transaction); // Database custom logging.
...
SessionLog.start(session); // Session custom logging.
...
} finally {
LogContext.exit(myLog);
}[/code]
Modifier and Type | Field and Description |
---|---|
static Class<? extends LogContext> |
CONSOLE
Holds a context logging debug/informative/warnings/errors events to
the system console (JVM 1.6+).
|
static Configurable<Class<? extends LogContext>> |
DEFAULT
Holds the logging context default implementation (configurable,
default value
STANDARD ). |
static Class<? extends LogContext> |
NULL
Holds a logging context implementation ignoring logging events.
|
static Class<? extends LogContext> |
STANDARD
Holds the logging context implementation forwarding log events to the
root
java.util.logging.Logger (default logging context). |
static Class<? extends LogContext> |
SYSTEM_OUT
Holds a context logging debug/informative/warning/error messages
to
System.out . |
Modifier | Constructor and Description |
---|---|
protected |
LogContext()
Default constructor.
|
Modifier and Type | Method and Description |
---|---|
static void |
debug(CharSequence message)
Logs the specified debug message if debug messages are logged.
|
static void |
debug(Object... messages)
Equivalent to
debug(CharSequence) except that formatting
is done only if debug is logged. |
static void |
debug(Object message)
Equivalent to
debug(CharSequence) except that formatting
is done only if debug is logged. |
protected void |
enterAction()
The action to be performed after this context becomes the current
context.
|
static void |
error(CharSequence message)
Logs the specified error message to the current logging
context.
|
static void |
error(Object... messages)
Equivalent to
error(CharSequence)
except that formatting is done only if error is logged. |
static void |
error(Object message)
Equivalent to
error(CharSequence) except that formatting
is done only if error is logged. |
static void |
error(Throwable error)
Logs the specified error to the current logging context.
|
static void |
error(Throwable error,
CharSequence message)
Logs the specified error and error message to the current logging
context.
|
static void |
error(Throwable error,
Object... messages)
Equivalent to
error(Throwable, CharSequence)
except that formatting is done only if error is logged. |
static void |
error(Throwable error,
Object message)
Equivalent to
error(Throwable, CharSequence) except that
formatting is done only if error is logged. |
protected void |
exitAction()
The action to be performed before this context is no more the current
context.
|
static LogContext |
getCurrentLogContext()
Returns the current logging context.
|
static LogContext |
getDefault()
Returns the default instance (
DEFAULT implementation). |
static void |
info(CharSequence message)
Logs the specified informative message.
|
static void |
info(Object... messages)
Equivalent to
info(CharSequence) except that formatting
is done only if info is logged. |
static void |
info(Object message)
Equivalent to
info(CharSequence) except that formatting
is done only if info is logged. |
static boolean |
isDebugLogged()
Indicates if debug messages are currently logged.
|
static boolean |
isErrorLogged()
Indicates if error messages are currently logged.
|
static boolean |
isInfoLogged()
Indicates if info messages are currently logged.
|
protected boolean |
isLogged(String category)
Indicates if the messages of the specified category are being logged
(default
true all messages are being logged). |
static boolean |
isWarningLogged()
Indicates if warning messages are currently logged.
|
protected void |
logDebug(CharSequence message)
Logs the specified debug message.
|
protected void |
logError(Throwable error,
CharSequence message)
Logs the specified error.
|
protected void |
logInfo(CharSequence message)
Logs the specified informative message.
|
protected abstract void |
logMessage(String category,
CharSequence message)
Logs the message of specified category (examples of category are
"debug", "info", "warning", "error").
|
protected void |
logWarning(CharSequence message)
Logs the specified warning message.
|
static void |
warning(CharSequence message)
Logs the specified warning message.
|
static void |
warning(Object... messages)
Equivalent to
warning(CharSequence) except that formatting
is done only if warning is logged. |
static void |
warning(Object message)
Equivalent to
warning(CharSequence) except that formatting
is done only if warning is logged. |
enter, enter, exit, exit, getCurrentContext, getOuter, getOwner, setConcurrentContext, toString
public static final Class<? extends LogContext> STANDARD
java.util.logging.Logger
(default logging context).
The debug/info/warning/error events are mapped to the
debug/info/warning/severe log levels respectively.public static final Class<? extends LogContext> NULL
public static final Class<? extends LogContext> SYSTEM_OUT
System.out
.public static final Class<? extends LogContext> CONSOLE
public static final Configurable<Class<? extends LogContext>> DEFAULT
STANDARD
).public static LogContext getCurrentLogContext()
getDefault()
is returned.public static LogContext getDefault()
DEFAULT
implementation).public static boolean isDebugLogged()
true
if debug messages are logged;
false
otherwise.public static void debug(CharSequence message)
message
- the debug message being logged.logDebug(CharSequence)
public static void debug(Object message)
debug(CharSequence)
except that formatting
is done only if debug is logged.message
- the message to log.public static void debug(Object... messages)
debug(CharSequence)
except that formatting
is done only if debug is logged.messages
- the messages to log.public static boolean isInfoLogged()
true
if info messages are logged;
false
otherwise.public static void info(CharSequence message)
message
- the informative message being logged.logInfo(CharSequence)
public static void info(Object message)
info(CharSequence)
except that formatting
is done only if info is logged.message
- the message to log.public static void info(Object... messages)
info(CharSequence)
except that formatting
is done only if info is logged.messages
- the messages to log.public static boolean isWarningLogged()
true
if warning messages are logged;
false
otherwise.public static void warning(CharSequence message)
message
- the warning message being logged.logWarning(CharSequence)
public static void warning(Object message)
warning(CharSequence)
except that formatting
is done only if warning is logged.message
- the message to log.public static void warning(Object... messages)
warning(CharSequence)
except that formatting
is done only if warning is logged.messages
- the messages to log.public static boolean isErrorLogged()
true
if error messages are logged;
false
otherwise.public static void error(Throwable error)
error
- the error being logged.public static void error(Throwable error, CharSequence message)
error
- the error being logged.message
- the supplementary message.public static void error(Throwable error, Object message)
error(Throwable, CharSequence)
except that
formatting is done only if error is logged.error
- the error being logged.message
- the supplementary message.public static void error(Throwable error, Object... messages)
error(Throwable, CharSequence)
except that formatting is done only if error is logged.error
- the error being logged.messages
- the supplementary messages.public static void error(CharSequence message)
message
- the error message being logged.public static void error(Object message)
error(CharSequence)
except that formatting
is done only if error is logged.message
- the message to log.public static void error(Object... messages)
error(CharSequence)
except that formatting is done only if error is logged.messages
- the messages to log.protected abstract void logMessage(String category, CharSequence message)
category
- an identifier of the category of the messages logged.message
- the message itself.protected boolean isLogged(String category)
true
all messages are being logged).
Note: This method is an indicator only, not a directive.
It allows users to bypass the logging processing if no
actual logging is performed. If the category is not
known then this method should return true
(no optimization performed).
category
- an identifier of the category for the messages logged.true
if the messages of the specified category
are being logged; false
otherwise.protected void logDebug(CharSequence message)
message
- the debug message to be logged.logMessage(java.lang.String, java.lang.CharSequence)
protected void logInfo(CharSequence message)
message
- the informative message to be logged.protected void logWarning(CharSequence message)
message
- the warning message to be logged.protected void logError(Throwable error, CharSequence message)
logMessage("", message + stackTrace)
.error
- the error being logged or null
if none.message
- the associated message or null
if none.protected void enterAction()
Context
enterAction
in class Context
protected void exitAction()
Context
exitAction
in class Context
Copyright © 2005–2018 Javolution. All rights reserved.