com.adventnet.afp.log
Class LogFactory

java.lang.Object
  |
  +--com.adventnet.afp.log.LogFactory

public class LogFactory
extends java.lang.Object

The Logging Framework is governed by the LogFactory. It manages the LogWriters and
the Loggers. This class manages the printing of log out and log error messages. The
LogFactory reads the initial configuration details from the LogConfigReader. After
getting the details, it creates the Loggers and LogWriters. In the LogFactory, the
Log writers and their corresponding Loggers are stored.

The LogFactory carries out a check for the Logger class name and LogWriter class
name. If the class names are null, the default class names will be taken. The Loggers
and LogWriters are created only at the time of logging.  Loggers in turn create
LogWriters. The LogFactory facilitates adding LogWriters and Loggers at runtime.
 If you want the runtime addition of the LogWriters and Loggers to be included in
the configuration itself, then it can also be done. Loggers and LogWriters can be
removed at Runtime. These changes can also be included in the configuration, if you
so wish through the LogConfigWriter.

Getting Started

This section explains how you can start working with the Logging Framework. The following code snippet explains the procedure in detail. For ease of reference, explanation has been provided alongside the code snippet itself.
 
/**
 * An example file which explains how to start with Logging framework and how to log the messages to a log file with the default implementation. The default Configuration reader is the LogConfigFileReader which reads the details from the file as properties. Add the following xml tags in the configuration file logging.xml file.

 

*<LOGGER>
*<InstanceName>TestLogger</InstanceName>
*<WriterInstanceName>SERVEROUT</WriterInstanceName> 
*<LogLevel>3</LogLevel>
*<PROPERTIES>
*     <PROPERTY>
*            <Key>DisplayName</Key>
*            <Value>MyLogger</Value>
*     </PROPERTY>
*</PROPERTIES> 
*</LOGGER>


 
 

 
//To create a Logger with instance name "TestLogger" 
 

public class Example
{
    public static void main(String args[]) throws LogException
    {

// Before initializing the LogFactory, configuration details for Logging have to be read by the LogConfigReader from the destination where the details are available. So, mention the destination.
 

// NOTE : Replace the parameters with the actual directory and fileName.

     LogConfigReader reader = new LogConfigFileReader("G:/log/logging.xml");
     LogConfigWriter writer = new LogConfigFileWriter("G:/log/logging.xml"); 

//To get the Instance of the LogFactory
       LogFactory factory = LogFactory.getInstance();

//To initialize the LogFactory. 
        factory.init(reader , writer);

//Get the Logger instance of the example Logger . Only after this, the Logger will be created.
        Logger logger = factory.getLogger("TestLogger");

//To set the LogLevel of the Logger to some desired value.
        logger.setLogLevel(LogLevel.LEVEL3);

// Log some messages with a level less than the Logger's level. The message will be displayed in the following format <TimeStamp>:<DisplayName>:<Message>. Here, in this example, the displayName will be "MyLogger"
        for (int i = 0; i < 100; i++)
        {

// Here the InstanceName of Logger is 'SERVEROUT'. So, the log messages will be displayed in the Writer which has the same InstanceName. (i.e)'SERVEROUT'.

            logger.out("This is message with number : " + i, LogLevel.LEVEL1);
            logger.err("This is error message with number : " + i, new Exception(),
LogLevel.LEVEL1);

        }
 

// To disable the logging and then to log some messages
        logger.setLogLevel(LogLevel.DISABLE);

// Since Logging is disabled, the following messages will not be logged in the respective Writer.

        for (int i = 100; i < 200; i++)

        {
            logger.out("This is the disabled message with number : " + i, LogLevel.LEVEL1);
            logger.err("This is disabled error message with number : " + i, new Exception(),
LogLevel.LEVEL1);

        }
    }


Method Summary
 Logger createLogger(LoggerProperties loggerProps, boolean updateInConfFile)
          The createLogger method is used to add a Logger.
 LogWriter createLogWriter(LogWriterProperties logWriterProps, boolean updateInConfFile)
          The method createLogWriters is used to create a new LogWriter.
static LogFactory getInstance()
          Used to get the reference of LogFactory.
 Logger getLogger(java.lang.String loggerInstanceName)
          The getLogger method is used to get the instance of the Logger specified by loggerInstanceName.
 java.util.Hashtable getLoggers()
          This method is used to get the instances of all the Loggers that are created for performing the Logging operation.
 LogWriter getLogWriter(java.lang.String logWriterInstanceName)
          Gives instance of the LogWriter which has the logWriterInstanceName.
 java.util.Hashtable getLogWriters()
          This method is used to get the instance of all the LogWriters that are created for performing the Logging operation.
 void init(LogConfigReader reader, LogConfigWriter writer)
           
 void removeLogger(java.lang.String loggerInstanceName, boolean updateInConfFile)
          The removeLogger method is used to remove a Logger from the Logfactory.
 void removeLogWriter(java.lang.String logWriterInstanceName, boolean updateInConfFile)
          The removeLogWriter method is used to remove a LogWriter.
 void stop()
          Method for stopping Loggers.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Method Detail

init

public void init(LogConfigReader reader,
                 LogConfigWriter writer)
          throws LogException

getInstance

public static LogFactory getInstance()
Used to get the reference of LogFactory. Once LogFactory is initialized this method can be used to get the reference
Returns:
LogFactory reference

stop

public void stop()
          throws LogException
Method for stopping Loggers. It will be better to call this method at the end of the application to free up some of the resources used by logging module as well as flushing of all cached contents. All the Loggers and the Logwriters will be dropped
Throws:
LogException - if an error occurs in removing the Loggers or LogWriters

createLogWriter

public LogWriter createLogWriter(LogWriterProperties logWriterProps,
                                 boolean updateInConfFile)
                          throws LogException
The method createLogWriters is used to create a new LogWriter. The properties of the module are specified in the LogWriter. The method returns true only if all the Loggers in the module are created properly. False will be returned if LogWriter could not be created.
Parameters:
logWriterProps - specifies the properties of the LogWriter and Loggers.
updateInConfFile - denotes whether the properties of the newly added LogWriter and its Loggers are to be writen to the logging configuration file.
Throws:
LogException - if an error occurs during the creation of a Logger/LogWriter or while processing the XML file.

removeLogWriter

public void removeLogWriter(java.lang.String logWriterInstanceName,
                            boolean updateInConfFile)
                     throws LogException
The removeLogWriter method is used to remove a LogWriter. This method can be called when the LogWriter and all the Loggers asscociated with it are no longer needed. On calling this method, the contents will be flushed and all the Loggers that used the LogWriter will be removed.
Parameters:
logWriterInstanceName - a String value denoting the LogWriter to be removed
updateInConfFile - a boolean value denoting if the change has to be done in the conf file.
Throws:
LogException - if an error occurs during the removal of a LogWriter or during processing of Conf file.

createLogger

public Logger createLogger(LoggerProperties loggerProps,
                           boolean updateInConfFile)
                    throws LogException
The createLogger method is used to add a Logger. The Logger will be created only if there are no other Loggers with the same LoggerInstanceName. Otherwise, LogException will be thrown.
Parameters:
loggerProps - specifies the properties for the Logger
updateInConfFile - a boolean value denoting whether the newly created Logger has to be written to the logging configuration file.
Throws:
LogException - if an error occurs during the creation of the Logger

removeLogger

public void removeLogger(java.lang.String loggerInstanceName,
                         boolean updateInConfFile)
                  throws LogException
The removeLogger method is used to remove a Logger from the Logfactory. This can be called when the Logger is no longer needed. On calling this method, the contents of the LogWriter of that Logger will be flushed and then the Logger will be destroyed.
Parameters:
loggerInstanceName - a String value denoting the Logger to be removed
updateInConfFile - a boolean value denoting if the change has to be updated in the conf file
Throws:
LogException - if an error occurs while removing the Logger or updating the conf file.

getLogWriter

public LogWriter getLogWriter(java.lang.String logWriterInstanceName)
                       throws LogException
Gives instance of the LogWriter which has the logWriterInstanceName. It checks whether the LogWriter which has this instance name is created already. If it is not created, it will create the LogWriter.
Parameters:
logWriterInstanceName - Key for which log file writer is to be returned
Returns:
LogWriter instance

getLogger

public Logger getLogger(java.lang.String loggerInstanceName)
                 throws LogException
The getLogger method is used to get the instance of the Logger specified by loggerInstanceName. It will check whether the Logger with this instance name is already created. if the logger is not created, it will check whether the corresponding writer is created. If the writer is not created, it will create the Writer and then Logger is created. After creating the Logger and corresponding writer, it returns the instance of the Logger. The Logger instance can be used for changing the Logger's attributes at runtime.
Parameters:
loggerInstanceName - a String value denoting the Logger's unique InstanceName
Returns:
the Logger instance.

getLoggers

public java.util.Hashtable getLoggers()
This method is used to get the instances of all the Loggers that are created for performing the Logging operation.
Returns:
a Hashtable containing the instance of all the Loggers with their LoggerInstanceName as key.

getLogWriters

public java.util.Hashtable getLogWriters()
This method is used to get the instance of all the LogWriters that are created for performing the Logging operation.
Returns:
a Hashtable with the LogWriter instance with key as LogWriterInstanceName


Copyright (c)AdventNet Inc., 1996-2004