4 Handler and Output Format

4.1 Output Destination

The Common Logging Framework allows logging requests to print to multiple destinations. By default logging requests are either sent
to the console or to a file according to the value assigned to the property com.hp.mw.common.utils.logging.factory.output: Expect for the simple commonLogger implementation provided by this distribution, the CLF relies on the underlying logging, when used,
to manage the output via the appropriate class or interface they define.  For instance, log4j provide the Appender interface and the set of classes implementing this interface. Among these classes there are the ConsoleAppender class, which sends log events to the console, or the FileAppender class, which sends the log requests to the file. Similarly, the HP logging service provides the LogWriter interface, and the classes ConsoleLogWriter or TextFileLogWriter.

Once created by LogManager, the LoggerFactory is associated with an appropriate “output class ”. By default any commonLogger object created by that LoggerFactory is associated with an output class that sends logging messages to the console.  The application can send logging messages to the file using the property output as follow:

java –Dcom.hp.mw.common.utils.logging.factory.output=<output_value> MyApplication
Where the property output_value may have one of the values described above: console or file
 

Choosing its own output

As said above logging messages are by default sent to the console. An application may require to send logging messages to other outputs in addition to the console or instead the console. Two ways are provided for this aim:

How to add a Handler?

Two classes are provided to the application to define additional output classes: Let us give examples to illustrate how these classes are used.
1) Add a LogWriter
As seen above a created commonLogger sends log messages to the console. The CSF logging service has defined a class named TextFileLogWriter, which implements the LogWriter interface. The following example illustrates how an application adds a TextFileLogWriter object to the commonLogger to send log messages to a file in addition to the console.
 
 
. . .
import com.hp.mwlabs.common.util.logging.csf.CommonLogWriter;
import com.hp.mwlabs.common.util.logging.TextFileLogWriter;

import java.io.File;

public class AddLogWriter{

    static LoggerFactory log_fac = LogManager.getLogFactory();
    static commonLogger mylog = (commonLogger)log_fac.getLogger("TestAddLogWriter");
    static CommonLogWriter MyLogWriter;
    static TextFileLogWriter filewriter;
     . . . 
     java.io.File the_directory = new File(“MyDirectory”); 
     filewriter = new TextFileLogWriter(); // (1)
     filewriter.setName(“MyLogFile”);
     filewriter.setDirectory(the_directory);
     MyLogWriter = new CommonLogWriter(filewriter); // (2)
     mylog.addHandler(MyLogWriter);  // (3) 
     mylog.log(CommonLevel.FATAL, "This is the first FATAL Message");//(4)
     mylog.removeHandler(MyLogWriter); // (5)
     log_fac.releaseResource();
    ... 
}

The application creates a TextFileLogWriter object (1) and uses it as argument to create a CommonLogWriter object (2), which implements the Handler interface. The CommonLogWriter object is registered with the commonLogger object (3). The commonLogger object will use all Handler or output class registered to forward the log message (4).  Before leaving, the application un-registers the Handler from the commonLogger object.

2) Add an Appender
Log4j defines a class named FileAppender, which implements the Appender interface. The following example illustrates how an application adds a FileAppender object to the commonLogger to send log messages to a file in addition to the console.
 
. . .
import org.apache.log4j.PatternLayout;
import org.apache.log4j.FileAppender;
import com.hp.mw.common.util.logging.log4j.CommonAppender;

public class AddAppender{

   static LoggerFactory log_fac = LogManager.getLogFactory();
   static commonLogger mylog = (commonLogger)log_fac.getLogger("TestAddAppender");
   static CommonAppender MyAppender;
   static FileAppender fileAppender;
   static PatternLayout theLayout;

    theLayout = new PatternLayout(PatternLayout.TTCC_CONVERSION_PATTERN);// (1)
    fileAppender = new FileAppender(theLayout, “theLogFileName”); // (2)
    MyAppender = new CommonAppender(fileAppender); // (3)
    mylog.addHandler(MyAppender);  //  (4)

   mylog.log(CommonLevel.FATAL, "This is the first FATAL Message"); // (5)
   log_fac.releaseResource();
   . . . 
}

The log4j.PatternLayout is a particular class implementing the log4j.Layout responsible to format a message before sending it to output (1). The application uses the created PatternLayout object to instantiate a FileAppender object (2). That all log messages sent to the file managed by the FileAppender object will be printed using the format defined for the PatternLayout object. The FileAppender object is used as an argument to instantiate a CommonAppender object (3), which implements the Handler interface. The CommonAppender object is registered with the commonLogger object (4). The commonLogger object will use all Handler or output class registered to forward the log message (5).

3) Remove/Add the default output
As described above, a default output destination can be defined by specifying the value of the “output” property – “console” or “file”. However an application may wish to remove this default output in such that only its own output provided via the method addHandler on the commonLogger interface. For this aim, the commonLogger interface provides two methods to remove the default output destination from the list of output destination and to add if needed, after removing, the default output; respectively these methods are:

void removeDefaultOutput();
and
void addDefaultOutput();

4.2 Message Formatting

Similarly to the output destination, formatting log messages before their sending to the output relies on interface or classes provided by the underlying logging service. That is, an application may use provided classes to format the message, as done in the previous example where the application uses the PatternLayout class, or develop its own classes but implementing interface defined by the underlying service.

When using the provided simple commonLogger the output generated is as follow:

{Date} - {Time} - {Log_level} - {Log_Message_with_arguments_if_provided} - {Throwable_message_if_any}
Example: The following logging request
mylogger.warn("This is a Warning Message");
will produce, if the WARN level is enabled
February 3, 2003 - 12:44:24 PM - [WARN]  - This is a Warning Message
 
 
Main        Previous        Next