/*
Logging In Java with the JDK 1.4 Logging API and Apache log4j
by Samudra Gupta
Apress Copyright 2003
ISBN:1590590996
*/
import java.util.logging.*;
import java.io.*;
class CustomFormatter extends Formatter
{
public CustomFormatter(){}
/**
This method formats the given log record, in a java properties file style
*/
public synchronized String format(LogRecord record)
{
String methodName = record.getSourceMethodName();
String message = record.getMessage();
StringBuffer buffer = new StringBuffer(50);
buffer.append(methodName);
buffer.append("=");
buffer.append(message);
return buffer.toString();
}
}
public class CustomFormatterTest{
private static Logger logger = Logger.getLogger("sam.logging");
private String fileName = null;
public CustomFormatterTest(String fileName)
{
this.fileName = fileName;
try
{
FileHandler fh = new FileHandler(fileName);
CustomFormatter formatter = new CustomFormatter();
fh.setFormatter(formatter);
logger.addHandler(fh);
}catch(IOException ioe)
{
ioe.printStackTrace();
}
}
/**
* This method performs the logging activity
*/
public void logMessage()
{
logger.info("log this message");
}
public static void main(String args[]) {
CustomFormatterTest test = new CustomFormatterTest("name");
test.logMessage();
}
}
|