javolution.context
Class LogContext

Object
  extended by Context
      extended by LogContext
All Implemented Interfaces:
Serializable, XMLSerializable
Direct Known Subclasses:
StandardLog, TestContext

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:

         if (LogContext.isInfoLogged())
             LogContext.info("message part 1" + aVar + "message part 2");
Or equivalent but simpler:
         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);
     }

Version:
5.3, March 5, 2009
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Field Summary
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.
 
Fields inherited from class Context
ROOT
 
Constructor Summary
protected LogContext()
          Default constructor.
 
Method Summary
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.
 
Methods inherited from class Context
enter, enter, exit, exit, getCurrentContext, getOuter, getOwner, setConcurrentContext, toString
 
Methods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

STANDARD

public static final Class<? extends LogContext> STANDARD
Holds the logging context implementation forwarding log events to the root java.util.logging.Logger (default logging context). The debug/info/warning/error events are mapped to the debug/info/warning/severe log levels respectively.


NULL

public static final Class<? extends LogContext> NULL
Holds a logging context implementation ignoring logging events.


SYSTEM_OUT

public static final Class<? extends LogContext> SYSTEM_OUT
Holds a context logging debug/informative/warning/error messages to System.out.


CONSOLE

public static final Class<? extends LogContext> CONSOLE
Holds a context logging debug/informative/warnings/errors events to the system console (JVM 1.6+).


DEFAULT

public static final Configurable<Class<? extends LogContext>> DEFAULT
Holds the logging context default implementation (configurable, default value STANDARD).

Constructor Detail

LogContext

protected LogContext()
Default constructor.

Method Detail

getCurrentLogContext

public static LogContext getCurrentLogContext()
Returns the current logging context. If the current thread has not entered any logging context the getDefault() is returned.

Returns:
the current logging context.

getDefault

public static LogContext getDefault()
Returns the default instance (DEFAULT implementation).

Returns:
the default instance.

isDebugLogged

public static boolean isDebugLogged()
Indicates if debug messages are currently logged.

Returns:
true if debug messages are logged; false otherwise.

debug

public static void debug(CharSequence message)
Logs the specified debug message if debug messages are logged.

Parameters:
message - the debug message being logged.
See Also:
logDebug(CharSequence)

debug

public static void debug(Object message)
Equivalent to debug(CharSequence) except that formatting is done only if debug is logged.

Parameters:
message - the message to log.

debug

public static void debug(Object... messages)
Equivalent to debug(CharSequence) except that formatting is done only if debug is logged.

Parameters:
messages - the messages to log.

isInfoLogged

public static boolean isInfoLogged()
Indicates if info messages are currently logged.

Returns:
true if info messages are logged; false otherwise.

info

public static void info(CharSequence message)
Logs the specified informative message.

Parameters:
message - the informative message being logged.
See Also:
logInfo(CharSequence)

info

public static void info(Object message)
Equivalent to info(CharSequence) except that formatting is done only if info is logged.

Parameters:
message - the message to log.

info

public static void info(Object... messages)
Equivalent to info(CharSequence) except that formatting is done only if info is logged.

Parameters:
messages - the messages to log.

isWarningLogged

public static boolean isWarningLogged()
Indicates if warning messages are currently logged.

Returns:
true if warning messages are logged; false otherwise.

warning

public static void warning(CharSequence message)
Logs the specified warning message.

Parameters:
message - the warning message being logged.
See Also:
logWarning(CharSequence)

warning

public static void warning(Object message)
Equivalent to warning(CharSequence) except that formatting is done only if warning is logged.

Parameters:
message - the message to log.

warning

public static void warning(Object... messages)
Equivalent to warning(CharSequence) except that formatting is done only if warning is logged.

Parameters:
messages - the messages to log.

isErrorLogged

public static boolean isErrorLogged()
Indicates if error messages are currently logged.

Returns:
true if error messages are logged; false otherwise.

error

public static void error(Throwable error)
Logs the specified error to the current logging context.

Parameters:
error - the error being logged.

error

public static void error(Throwable error,
                         CharSequence message)
Logs the specified error and error message to the current logging context.

Parameters:
error - the error being logged.
message - the supplementary message.

error

public static void error(Throwable error,
                         Object message)
Equivalent to error(Throwable, CharSequence) except that formatting is done only if error is logged.

Parameters:
error - the error being logged.
message - the supplementary message.

error

public static void error(Throwable error,
                         Object... messages)
Equivalent to error(Throwable, CharSequence) except that formatting is done only if error is logged.

Parameters:
error - the error being logged.
messages - the supplementary messages.

error

public static void error(CharSequence message)
Logs the specified error message to the current logging context.

Parameters:
message - the error message being logged.

error

public static void error(Object message)
Equivalent to error(CharSequence) except that formatting is done only if error is logged.

Parameters:
message - the message to log.

error

public static void error(Object... messages)
Equivalent to error(CharSequence) except that formatting is done only if error is logged.

Parameters:
messages - the messages to log.

logMessage

protected abstract void logMessage(String category,
                                   CharSequence message)
Logs the message of specified category (examples of category are "debug", "info", "warning", "error").

Parameters:
category - an identifier of the category of the messages logged.
message - the message itself.

isLogged

protected boolean isLogged(String category)
Indicates if the messages of the specified category are being logged (default 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).

Parameters:
category - an identifier of the category for the messages logged.
Returns:
true if the messages of the specified category are being logged; false otherwise.

logDebug

protected void logDebug(CharSequence message)
Logs the specified debug message.

Parameters:
message - the debug message to be logged.
See Also:
logMessage(java.lang.String, java.lang.CharSequence)

logInfo

protected void logInfo(CharSequence message)
Logs the specified informative message.

Parameters:
message - the informative message to be logged.

logWarning

protected void logWarning(CharSequence message)
Logs the specified warning message.

Parameters:
message - the warning message to be logged.

logError

protected void logError(Throwable error,
                        CharSequence message)
Logs the specified error. The default implementation logs the message and the error stack trace (calls logMessage("", message + stackTrace).

Parameters:
error - the error being logged or null if none.
message - the associated message or null if none.

enterAction

protected void enterAction()
Description copied from class: Context
The action to be performed after this context becomes the current context.

Specified by:
enterAction in class Context

exitAction

protected void exitAction()
Description copied from class: Context
The action to be performed before this context is no more the current context.

Specified by:
exitAction in class Context


Copyright © 2005-2012 Javolution. All Rights Reserved.