J avolution v5.2 (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.

The default logging context is StandardLog to leverage java.util.logging capabilities.

Logging can be temporarily modified 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.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 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.2, August 5, 2007
Author:
Jean-Marie Dautelle
See Also:
Serialized Form

Field Summary
static java.lang.Class<? extends LogContext> CONSOLE
          Holds a context logging informative/warnings/errors events to the system console.
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 messages to System.out.
 
Fields inherited from class javolution.context.Context
ROOT
 
Constructor Summary
protected LogContext()
          Default constructor.
 
Method Summary
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 LogContext 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).
abstract  boolean isErrorLogged()
          Indicates if errors are logged.
abstract  boolean isInfoLogged()
          Indicates if informative messages are logged.
abstract  boolean isWarningLogged()
          Indicates if warning messages are logged.
abstract  void logError(java.lang.Throwable error, java.lang.CharSequence message)
          Logs the specified error.
 void logError(java.lang.Throwable error, java.lang.String message)
          Equivalent to logError(Throwable, CharSequence) (for J2ME compatibility).
abstract  void logInfo(java.lang.CharSequence message)
          Logs the specified informative message.
 void logInfo(java.lang.String message)
          Equivalent to logInfo(CharSequence) (for J2ME compatibility).
abstract  void logWarning(java.lang.CharSequence message)
          Logs the specified warning message.
 void logWarning(java.lang.String message)
          Equivalent to logWarning(CharSequence) (for J2ME compatibility).
static void warning(java.lang.CharSequence message)
          Logs the specified warning message to the current logging context.
static void warning(java.lang.String message)
          Equivalent to warning(CharSequence) (for J2ME compatibility).
 
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 info/warning/error events are mapped to the 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 messages to System.out.


CONSOLE

public static final java.lang.Class<? extends LogContext> CONSOLE
Holds a context logging informative/warnings/errors events to the system console.


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

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


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)

warning

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


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


isInfoLogged

public abstract boolean isInfoLogged()
Indicates if informative messages are logged.

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

logInfo

public abstract void logInfo(java.lang.CharSequence message)
Logs the specified informative message.

Parameters:
message - the informative message being logged.

logInfo

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


isWarningLogged

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

Returns:
true if warnings message are logged; false otherwise.

logWarning

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

Parameters:
message - the warning message being logged.

logWarning

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


isErrorLogged

public abstract boolean isErrorLogged()
Indicates if errors are logged.

Returns:
true if errors are logged; false otherwise.

logError

public abstract void logError(java.lang.Throwable error,
                              java.lang.CharSequence message)
Logs the specified error.

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

logError

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


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.2 (J2SE 1.5+)

Copyright © 2005 - 2007 Javolution.