J avolution v5.3 (J2SE 1.5+)

javolution.context
Class LogContext

java.lang.Object
  extended by javolution.context.Context
      extended by javolution.context.LogContext
All Implemented Interfaces:
java.io.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 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.String message)
          Equivalent to debug(CharSequence) (for J2ME compatibility).
static void debug(java.lang.String message, java.lang.Object obj)
          Equivalent to debug(message + obj} except formatting is done only if debug is logged.
static void debug(java.lang.String messagePart1, java.lang.Object obj, java.lang.String messagePart2)
          Equivalent to info(messagePart1 + obj + messagePart2} except formatting is done only if debug is logged.
static void debug(java.lang.String messagePart1, java.lang.Object obj1, java.lang.String messagePart2, java.lang.Object obj2)
          Equivalent to debug(messagePart1 + obj + messagePart2 + obj2} except formatting is done only if debug is logged.
static void debug(java.lang.String messagePart1, java.lang.Object obj1, java.lang.String messagePart2, java.lang.Object obj2, java.lang.String messagePart3)
          Equivalent to debug(messagePart1 + obj + messagePart2 + obj2 + messagePart3} except 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.String message)
          Equivalent to error(CharSequence) (for J2ME compatibility).
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.String message)
          Equivalent to error(Throwable, CharSequence) (for J2ME compatibility).
protected  void exitAction()
          The action to be performed before this context is no more the current context.
static Context getCurrent()
          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.String message)
          Equivalent to info(CharSequence) (for J2ME compatibility).
static void info(java.lang.String message, java.lang.Object obj)
          Equivalent to info(message + obj} except formatting is done only if info is logged.
static void info(java.lang.String messagePart1, java.lang.Object obj, java.lang.String messagePart2)
          Equivalent to info(messagePart1 + obj + messagePart2} except formatting is done only if info is logged.
static void info(java.lang.String messagePart1, java.lang.Object obj1, java.lang.String messagePart2, java.lang.Object obj2)
          Equivalent to info(messagePart1 + obj + messagePart2 + obj2} except formatting is done only if info is logged.
static void info(java.lang.String messagePart1, java.lang.Object obj1, java.lang.String messagePart2, java.lang.Object obj2, java.lang.String messagePart3)
          Equivalent to info(messagePart1 + obj + messagePart2 + obj2 + messagePart3} except 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.String message)
          Equivalent to warning(CharSequence) (for J2ME compatibility).
static void warning(java.lang.String message, java.lang.Object obj)
          Equivalent to warning(message + object} except formatting is done only if warnings are logged.
static void warning(java.lang.String messagePart1, java.lang.Object obj, java.lang.String messagePart2)
          Equivalent to warning(messagePart1 + object + messagePart2} except formatting is done only if warnings are logged.
static void warning(java.lang.String messagePart1, java.lang.Object obj1, java.lang.String messagePart2, java.lang.Object obj2)
          Equivalent to warning(messagePart1 + obj + messagePart2 + obj2} except formatting is done only if warning is logged.
static void warning(java.lang.String messagePart1, java.lang.Object obj1, java.lang.String messagePart2, java.lang.Object obj2, java.lang.String messagePart3)
          Equivalent to warning(messagePart1 + obj + messagePart2 + obj2 + messagePart3} except formatting is done only if warning is logged.
 
Methods inherited from class javolution.context.Context
enter, enter, exit, exit, getOuter, getOwner, setCurrent, toString
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

STANDARD

public static final java.lang.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 java.lang.Class<? extends LogContext> NULL
Holds a logging context implementation ignoring logging events.


SYSTEM_OUT

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


CONSOLE

public static final java.lang.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<java.lang.Class<? extends LogContext>> DEFAULT
Holds the logging context default implementation (configurable, default value STANDARD).

Constructor Detail

LogContext

protected LogContext()
Default constructor.

Method Detail

getCurrent

public static Context getCurrent()
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(java.lang.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(java.lang.String message)
Equivalent to debug(CharSequence) (for J2ME compatibility).

Parameters:
message - the message to log.

debug

public static void debug(java.lang.String message,
                         java.lang.Object obj)
Equivalent to debug(message + obj} except formatting is done only if debug is logged.

Parameters:
message - the message to log.
obj - the object whose string representation is logged.

debug

public static void debug(java.lang.String messagePart1,
                         java.lang.Object obj,
                         java.lang.String messagePart2)
Equivalent to info(messagePart1 + obj + messagePart2} except formatting is done only if debug is logged.

Parameters:
messagePart1 - the first part of the message to log.
obj - the object whose string representation is logged.
messagePart2 - the second part of the message to log.

debug

public static void debug(java.lang.String messagePart1,
                         java.lang.Object obj1,
                         java.lang.String messagePart2,
                         java.lang.Object obj2)
Equivalent to debug(messagePart1 + obj + messagePart2 + obj2} except formatting is done only if debug is logged.

Parameters:
messagePart1 - the first part of the message to log.
obj1 - the first object whose string representation is logged.
messagePart2 - the second part of the message to log.
obj2 - the second object whose string representation is logged.

debug

public static void debug(java.lang.String messagePart1,
                         java.lang.Object obj1,
                         java.lang.String messagePart2,
                         java.lang.Object obj2,
                         java.lang.String messagePart3)
Equivalent to debug(messagePart1 + obj + messagePart2 + obj2 + messagePart3} except formatting is done only if debug is logged.

Parameters:
messagePart1 - the first part of the message to log.
obj1 - the first object whose string representation is logged.
messagePart2 - the second part of the message to log.
obj2 - the second object whose string representation is logged.
messagePart3 - the third part of the message 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(java.lang.CharSequence message)
Logs the specified informative message.

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

info

public static void info(java.lang.String message)
Equivalent to info(CharSequence) (for J2ME compatibility).

Parameters:
message - the message to log.

info

public static void info(java.lang.String message,
                        java.lang.Object obj)
Equivalent to info(message + obj} except formatting is done only if info is logged.

Parameters:
message - the message to log.
obj - the object whose string representation is logged.

info

public static void info(java.lang.String messagePart1,
                        java.lang.Object obj,
                        java.lang.String messagePart2)
Equivalent to info(messagePart1 + obj + messagePart2} except formatting is done only if info is logged.

Parameters:
messagePart1 - the first part of the message to log.
obj - the object whose string representation is logged.
messagePart2 - the second part of the message to log.

info

public static void info(java.lang.String messagePart1,
                        java.lang.Object obj1,
                        java.lang.String messagePart2,
                        java.lang.Object obj2)
Equivalent to info(messagePart1 + obj + messagePart2 + obj2} except formatting is done only if info is logged.

Parameters:
messagePart1 - the first part of the message to log.
obj1 - the first object whose string representation is logged.
messagePart2 - the second part of the message to log.
obj2 - the second object whose string representation is logged.

info

public static void info(java.lang.String messagePart1,
                        java.lang.Object obj1,
                        java.lang.String messagePart2,
                        java.lang.Object obj2,
                        java.lang.String messagePart3)
Equivalent to info(messagePart1 + obj + messagePart2 + obj2 + messagePart3} except formatting is done only if info is logged.

Parameters:
messagePart1 - the first part of the message to log.
obj1 - the first object whose string representation is logged.
messagePart2 - the second part of the message to log.
obj2 - the second object whose string representation is logged.
messagePart3 - the third part of the message 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(java.lang.CharSequence message)
Logs the specified warning message.

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

warning

public static void warning(java.lang.String message)
Equivalent to warning(CharSequence) (for J2ME compatibility).

Parameters:
message - the message to log.

warning

public static void warning(java.lang.String message,
                           java.lang.Object obj)
Equivalent to warning(message + object} except formatting is done only if warnings are logged.


warning

public static void warning(java.lang.String messagePart1,
                           java.lang.Object obj,
                           java.lang.String messagePart2)
Equivalent to warning(messagePart1 + object + messagePart2} except formatting is done only if warnings are logged.


warning

public static void warning(java.lang.String messagePart1,
                           java.lang.Object obj1,
                           java.lang.String messagePart2,
                           java.lang.Object obj2)
Equivalent to warning(messagePart1 + obj + messagePart2 + obj2} except formatting is done only if warning is logged.

Parameters:
messagePart1 - the first part of the message to log.
obj1 - the first object whose string representation is logged.
messagePart2 - the second part of the message to log.
obj2 - the second object whose string representation is logged.

warning

public static void warning(java.lang.String messagePart1,
                           java.lang.Object obj1,
                           java.lang.String messagePart2,
                           java.lang.Object obj2,
                           java.lang.String messagePart3)
Equivalent to warning(messagePart1 + obj + messagePart2 + obj2 + messagePart3} except formatting is done only if warning is logged.

Parameters:
messagePart1 - the first part of the message to log.
obj1 - the first object whose string representation is logged.
messagePart2 - the second part of the message to log.
obj2 - the second object whose string representation is logged.
messagePart3 - the third part of the message 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(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.

error

public static void error(java.lang.Throwable error,
                         java.lang.String message)
Equivalent to error(Throwable, CharSequence) (for J2ME compatibility).


error

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

Parameters:
message - the error message being logged.

error

public static final void error(java.lang.String message)
Equivalent to error(CharSequence) (for J2ME compatibility).


logMessage

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").

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

isLogged

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).

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(java.lang.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(java.lang.CharSequence message)
Logs the specified informative message.

Parameters:
message - the informative message to be logged.

logWarning

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

Parameters:
message - the warning message to be logged.

logError

protected void logError(java.lang.Throwable error,
                        java.lang.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

J avolution v5.3 (J2SE 1.5+)

Copyright © 2005 - 2009 Javolution.