Handler and Output Format

## 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:

- console – this the default value
- file

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:

- Use a configuration defined by the underlying logging. Most logging tools allows to define a set of properties via a configuration file. For instance log4j allows to use a Java properties file (default name log4j.properties) or XML file to define properties used by log4j to determine how to deal with logging requests.
Rather than to provide default properties that must be used by the underlying logging the Common Logging Framework allows to use a configuration file that can be used directly by the underlying logging. For this aim, the property com.hp.mw.common.utils.logging.factory.output take one the following values:

- config_key - this indicates that the properties shall be determined by reading the content of a configuration file (Java properties) used by the underlying logging.
- config_xml - this indicates that the properties shall be determined by reading the content of a configuration file (Java properties) used by the underlying logging.

- Use the method AddHandler provided by the commonLogger. This method takes as argument an object implementing the Handler interface. The Handler interface is an “empty” interface without methods and represents a generic name given for an output such a console or a text file. This interface is used to hide the nature of the similar underlying handler.

#### How to add a Handler?

Two classes are provided to the application to define additional output classes:

- The CommonLogWriter to build a Handler object associated to a LogWriter object recognized by the CSF logging service
- The CommonAppender to a Handler object associated to an Appender object recognized by log4j

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