Class LogContext
- java.lang.Object
-
- javolution37.javolution.realtime.Context
-
- javolution37.javolution.realtime.LogContext
-
- Direct Known Subclasses:
- StandardLog
public abstract class LogContext extends Context
This class represents a logging context; it allows for object-based/thread-based logging and logging specializations (such as
StandardLogto leveragejava.util.loggingcapabilities).The default logging context is
STANDARD(log events forwarded to the rootjava.util.logging.Logger). Users may changes the default logging for all threads:and/or perform custom logging for particular objects/threads:// Logging disabled by default. LogContext.setDefault(LogContext.NULL);LogContext.enter(LogContext.SYSTEM); try { LogContext.info("Writes this message to System.out"); ... } finally { LogContext.exit(LogContext.SYSTEM); }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.isInfoLogged()) { 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
Loggableabove) 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
Fields Modifier and Type Field and Description static LogContextNULLHolds a context ignoring logging events.static StandardLogSTANDARDHolds a context forwarding log events to the rootjava.util.logging.Loggerframework (default logging context).static LogContextSYSTEMHolds a context logging informative messages toSystem.outand warnings/errors events toSystem.err.static LogContextSYSTEM_ERRHolds a context logging warnings/errors events toSystem.errand ignoring informative messages.
-
Constructor Summary
Constructors Constructor and Description LogContext()Default constructor.
-
Method Summary
All Methods Static Methods Instance Methods Abstract Methods Concrete Methods Modifier and Type Method and Description static LogContextcurrent()Returns the current logging context (orgetDefault()if the current thread has not entered any logging context).static voiderror(java.lang.Throwable error)Logs the specified error to the current logging context.static voiderror(java.lang.Throwable error, java.lang.CharSequence message)Logs the specified error and error message to the current logging context.static LogContextgetDefault()static voidinfo(java.lang.CharSequence message)Logs the specified informative message.abstract booleanisErrorLogged()Indicates if errors are logged.abstract booleanisInfoLogged()Indicates if informative messages are logged.abstract booleanisWarningLogged()Indicates if warning messages are logged.abstract voidlogError(java.lang.Throwable error, java.lang.CharSequence message)Logs the specified error.abstract voidlogInfo(java.lang.CharSequence message)Logs the specified informative message.abstract voidlogWarning(java.lang.CharSequence message)Logs the specified warning message.static voidsetDefault(LogContext defaultLog)Sets the specified logging context as default.static voidwarning(java.lang.CharSequence message)Logs the specified warning message to the current logging context.
-
-
-
Field Detail
-
STANDARD
public static final StandardLog STANDARD
Holds a context forwarding log events to the rootjava.util.logging.Loggerframework (default logging context). The info/warning/error events are mapped to the info/warning/severe log levels respectively.
-
NULL
public static final LogContext NULL
Holds a context ignoring logging events.
-
SYSTEM
public static final LogContext SYSTEM
Holds a context logging informative messages toSystem.outand warnings/errors events toSystem.err.
-
SYSTEM_ERR
public static final LogContext SYSTEM_ERR
Holds a context logging warnings/errors events toSystem.errand ignoring informative messages.
-
-
Method Detail
-
current
public static LogContext current()
Returns the current logging context (orgetDefault()if the current thread has not entered any logging context).- Returns:
- the current logging context.
-
getDefault
public static LogContext getDefault()
- Returns:
- the default logging contexts.
-
setDefault
public static void setDefault(LogContext defaultLog)
Sets the specified logging context as default.- Parameters:
defaultLog- the default logging context.
-
info
public static void info(java.lang.CharSequence message)
Logs the specified informative message.- Parameters:
message- the informative message being logged.- See Also:
logInfo(CharSequence)
-
warning
public static void warning(java.lang.CharSequence message)
Logs the specified warning message to the current logging context.- Parameters:
message- the warning message being logged.- See Also:
logWarning(CharSequence)
-
error
public static void error(java.lang.Throwable error)
Logs the specified error to the current logging context.- Parameters:
error- the error being logged.
-
error
public static void error(java.lang.Throwable error, java.lang.CharSequence message)Logs the specified error and error message to the current logging context.- Parameters:
error- the error being logged.message- the supplementary message ornullif none.
-
isInfoLogged
public abstract boolean isInfoLogged()
Indicates if informative messages are logged.- Returns:
trueif informative messages are logged;falseotherwise.
-
logInfo
public abstract void logInfo(java.lang.CharSequence message)
Logs the specified informative message.- Parameters:
message- the informative message being logged.
-
isWarningLogged
public abstract boolean isWarningLogged()
Indicates if warning messages are logged.- Returns:
trueif warnings message are logged;falseotherwise.
-
logWarning
public abstract void logWarning(java.lang.CharSequence message)
Logs the specified warning message.- Parameters:
message- the warning message being logged.
-
isErrorLogged
public abstract boolean isErrorLogged()
Indicates if errors are logged.- Returns:
trueif errors are logged;falseotherwise.
-
logError
public abstract void logError(java.lang.Throwable error, java.lang.CharSequence message)Logs the specified error.- Parameters:
error- the error being logged.message- the associated message ornullif none.
-
-
DMelt 3.0 © DataMelt by jWork.ORG