This class represents a context for object-based/thread-based logging
capabilities.
The
LogContext.DEFAULT default logging context is
StandardLog StandardLog to leverage java.util.logging
capabilities.
Logging can be temporarily modified 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.isInfoLogged()){
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]
author: Jean-Marie Dautelle version: 5.2, August 5, 2007 |