JavaGantt 2011.1 API

eu.beesoft.gaia.log
Class LogFactory

java.lang.Object
  extended by eu.beesoft.gaia.log.LogFactory
Direct Known Subclasses:
ApacheLogFactory, JavaLogFactory, Log4jFactory

public abstract class LogFactory
extends java.lang.Object

Abstract superclass for all log factories. It serves as a factory of logger instances for different logging systems. Current factory can be obtained by method getInstance(). The typical usage scenario is like this:

 LogFactory factory = LogFactory.getInstance ();
 Log log = factory.getLog ("mypackage.MyClass");
 if (log.isDebugEnabled ()) {
        log.debug ("This is logged message");
 }
 

This class also offers methods setThreadBoundIdentifier(String) and getThreadBoundIdentifier() to create link between current thread and an identifier defined by programmer. You can use them to log records with user name, for example. This is useful in server applications, where is difficult to pair a thread to the active client.

To customize this class to the requested logging system, you have to override createLog(String) method for your needs and to subclass the AbstractLog class. There are prepared implementations for:

To use some of these logging systems you have to configure the underlaying real subsystem and create a corresponding factory by invoking its constructor (for example, for Log4J subsystem you need Log4jFactory instance). Then you can use the pattern from the introduction in your application.


Constructor Summary
protected LogFactory()
          Default constructor.
 
Method Summary
protected abstract  Log createLog(java.lang.String name)
          Creates a new instance of Log.
static LogFactory getInstance()
          Returns an instance of the LogFactory.
 Log getLog(java.lang.Class<?> clazz)
          Returns Log instance for given class.
 Log getLog(java.lang.Object object)
          Returns Log instance for class of the given object.
 Log getLog(java.lang.String name)
          Returns Log instance for given class name.
 java.lang.String getThreadBoundIdentifier()
          Returns an identifier bound to the current thread or null, if no one was set.
 void setThreadBoundIdentifier(java.lang.String id)
          Binds current thread with given identifier.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LogFactory

protected LogFactory()
Default constructor. Sets a current log factory instance (returned by getInstance() method).

Method Detail

getInstance

public static LogFactory getInstance()
Returns an instance of the LogFactory. If this instance was not created yet, tries to get system property log.factory.class to get class name of the factory. If property was not found, uses name of class JavaLogFactory. Then creates instance for found class and returns it.

Returns:
instance of LogFactory.
Throws:
java.lang.RuntimeException - if cannot create an instance

createLog

protected abstract Log createLog(java.lang.String name)
Creates a new instance of Log. This is invoked from getLog() method if log for given class(name) still does not exists.

Returns:
newly created instance of Log

getLog

public Log getLog(java.lang.String name)
Returns Log instance for given class name. If log does not exist, invokes createLog(String) to get one.

Parameters:
name - class name for which is log required
Returns:
Log instance for given class name

getLog

public Log getLog(java.lang.Class<?> clazz)
Returns Log instance for given class. Invokes getLog(String) to obtain return value.

Parameters:
clazz - class for which is log required
Returns:
Log instance for given class name

getLog

public Log getLog(java.lang.Object object)
Returns Log instance for class of the given object. Invokes getLog(String) to obtain return value.

Parameters:
object - - object for which class is log required
Returns:
Log instance for given object class

setThreadBoundIdentifier

public void setThreadBoundIdentifier(java.lang.String id)
Binds current thread with given identifier. This is useful when you need differ the log sources. For example, on server you need distinguish separate clients and at the same time to identify the same client accross different threads.This is where setThreadBoundIdentifier() comes into play.

Invoke this method when you start a new thread and deliver to it an identifier (such as client / user name, etc). The Log implementations will query for this thread / identifier mapping to append it to log record.

Parameters:
id - - identifier to bind with current thread

getThreadBoundIdentifier

public java.lang.String getThreadBoundIdentifier()
Returns an identifier bound to the current thread or null, if no one was set.

Returns:
an identifier bound to the current thread

JavaGantt 2011.1 API