|
J avolution v5.5 (J2SE 1.6+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |
java.lang.Objectjavolution.context.Context
javolution.context.LogContext
public abstract class LogContext
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:
Or equivalent but simpler:
if (LogContext.isInfoLogged())
LogContext.info("message part 1" + aVar + "message part 2");
LogContext.info("message part 1", aVar, "message part 2");
Logging can be temporarily altered on a thread or object basis.
For example:
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.
}
...
}
Applications may extend this base class to address specific logging
requirements. For example:
// 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);
}
}
The use of interfaces (such as Loggable
above) makes it easy
for any context to support customs logging events.
For example:
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);
}
Field Summary | |
---|---|
static java.lang.Class<? extends LogContext> |
CONSOLE
Holds a context logging debug/informative/warnings/errors events to the system console (JVM 1.6+). |
static Configurable<java.lang.Class<? extends LogContext>> |
DEFAULT
Holds the logging context default implementation (configurable, default value STANDARD ). |
static java.lang.Class<? extends LogContext> |
NULL
Holds a logging context implementation ignoring logging events. |
static java.lang.Class<? extends LogContext> |
STANDARD
Holds the logging context implementation forwarding log events to the root java.util.logging.Logger (default logging context). |
static java.lang.Class<? extends LogContext> |
SYSTEM_OUT
Holds a context logging debug/informative/warning/error messages to System.out . |
Fields inherited from class javolution.context.Context |
---|
ROOT |
Constructor Summary | |
---|---|
protected |
LogContext()
Default constructor. |
Method Summary | |
---|---|
static void |
debug(java.lang.CharSequence message)
Logs the specified debug message if debug messages are logged. |
static void |
debug(java.lang.Object... messages)
Equivalent to debug(CharSequence) except that formatting
is done only if debug is logged. |
static void |
debug(java.lang.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(java.lang.CharSequence message)
Logs the specified error message to the current logging context. |
static void |
error(java.lang.Object... messages)
Equivalent to error(CharSequence)
except that formatting is done only if error is logged. |
static void |
error(java.lang.Object message)
Equivalent to error(CharSequence) except that formatting
is done only if error is logged. |
static void |
error(java.lang.Throwable error)
Logs the specified error to the current logging context. |
static void |
error(java.lang.Throwable error,
java.lang.CharSequence message)
Logs the specified error and error message to the current logging context. |
static void |
error(java.lang.Throwable error,
java.lang.Object... messages)
Equivalent to error(Throwable, CharSequence)
except that formatting is done only if error is logged. |
static void |
error(java.lang.Throwable error,
java.lang.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(java.lang.CharSequence message)
Logs the specified informative message. |
static void |
info(java.lang.Object... messages)
Equivalent to info(CharSequence) except that formatting
is done only if info is logged. |
static void |
info(java.lang.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(java.lang.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(java.lang.CharSequence message)
Logs the specified debug message. |
protected void |
logError(java.lang.Throwable error,
java.lang.CharSequence message)
Logs the specified error. |
protected void |
logInfo(java.lang.CharSequence message)
Logs the specified informative message. |
protected abstract void |
logMessage(java.lang.String category,
java.lang.CharSequence message)
Logs the message of specified category (examples of category are "debug", "info", "warning", "error"). |
protected void |
logWarning(java.lang.CharSequence message)
Logs the specified warning message. |
static void |
warning(java.lang.CharSequence message)
Logs the specified warning message. |
static void |
warning(java.lang.Object... messages)
Equivalent to warning(CharSequence) except that formatting
is done only if warning is logged. |
static void |
warning(java.lang.Object message)
Equivalent to warning(CharSequence) except that formatting
is done only if warning is logged. |
Methods inherited from class javolution.context.Context |
---|
enter, enter, exit, exit, getCurrentContext, getOuter, getOwner, setConcurrentContext, toString |
Methods inherited from class java.lang.Object |
---|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait |
Field Detail |
---|
public static final java.lang.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 java.lang.Class<? extends LogContext> NULL
public static final java.lang.Class<? extends LogContext> SYSTEM_OUT
System.out
.
public static final java.lang.Class<? extends LogContext> CONSOLE
public static final Configurable<java.lang.Class<? extends LogContext>> DEFAULT
STANDARD
).
Constructor Detail |
---|
protected LogContext()
Method Detail |
---|
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(java.lang.CharSequence message)
message
- the debug message being logged.logDebug(CharSequence)
public static void debug(java.lang.Object message)
debug(CharSequence)
except that formatting
is done only if debug is logged.
message
- the message to log.public static void debug(java.lang.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(java.lang.CharSequence message)
message
- the informative message being logged.logInfo(CharSequence)
public static void info(java.lang.Object message)
info(CharSequence)
except that formatting
is done only if info is logged.
message
- the message to log.public static void info(java.lang.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(java.lang.CharSequence message)
message
- the warning message being logged.logWarning(CharSequence)
public static void warning(java.lang.Object message)
warning(CharSequence)
except that formatting
is done only if warning is logged.
message
- the message to log.public static void warning(java.lang.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(java.lang.Throwable error)
error
- the error being logged.public static void error(java.lang.Throwable error, java.lang.CharSequence message)
error
- the error being logged.message
- the supplementary message.public static void error(java.lang.Throwable error, java.lang.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(java.lang.Throwable error, java.lang.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(java.lang.CharSequence message)
message
- the error message being logged.public static void error(java.lang.Object message)
error(CharSequence)
except that formatting
is done only if error is logged.
message
- the message to log.public static void error(java.lang.Object... messages)
error(CharSequence)
except that formatting is done only if error is logged.
messages
- the messages to log.protected abstract void logMessage(java.lang.String category, java.lang.CharSequence message)
category
- an identifier of the category of the messages logged.message
- the message itself.protected boolean isLogged(java.lang.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(java.lang.CharSequence message)
message
- the debug message to be logged.logMessage(java.lang.String, java.lang.CharSequence)
protected void logInfo(java.lang.CharSequence message)
message
- the informative message to be logged.protected void logWarning(java.lang.CharSequence message)
message
- the warning message to be logged.protected void logError(java.lang.Throwable error, java.lang.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
|
J avolution v5.5 (J2SE 1.6+) | ||||||||
PREV CLASS NEXT CLASS | FRAMES NO FRAMES | ||||||||
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD |