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> MyApplicationWhere the property output_value may have one of the values described above: console or file
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();andvoid addDefaultOutput();
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 MessageMain Previous Next