Home Java Example Java Util Creating a Custom Formatter for a Logger Handler in Java
Questions:Ask|Latest


 
 

Share on Google+Share on Google+

Creating a Custom Formatter for a Logger Handler in Java

Advertisement
This section tells you how to create a custom formatter for a logger handler. Java provides two types formatter SimpleFormatter and XMLFormatter.

Creating a Custom Formatter for a Logger Handler in Java

     

This section tells you how to create a custom formatter for a logger handler. Java provides two types formatter SimpleFormatter and XMLFormatter. But, the java logging package allows for creating a custom formatter through the logger handler. Here you will see custom formatter that means formatter is created by users or user defined formatter. A logger handler writes log records to another file by using the logger formatter.

Descriptions of program:

Program creates a logger and writes logger records to given file in FileHandler that has append property is true to add content into last of file. Contents are adding into a file to given format in custom formatter. Here, formatter provides the facility to write date into file in 'heading 1' format.

Description of code:

getHead(Handler h):
Above is a method of Formatter class that returns string types formatter records. This method takes a argument which is any type of handler.

Here is the code of program:

import java.io.*;
import java.util.*;
import java.util.logging.*;

public class CustomFormatter extends XMLFormatter{
  public static void main(String[] args) {
  Logger log = Logger.getLogger("RoseIndia.Net");
  try{
  FileHandler hand = new FileHandler("vinod.html"true);
  hand.setFormatter(new CustomFormatter());
  log.addHandler(hand);
  log.info("The Custom formatter is created by RoseIndia.Net");
  }
  catch (IOException e){}
  }
  public String format(LogRecord rec) {
  StringBuffer buf = new StringBuffer(1000);
  buf.append(formatMessage(rec));
  return buf.toString();
  }
 public String getHead(Handler h) {
  return "<HTML><HEAD><h1>"+(new Date())+"</h1></HEAD><BODY>\n";
 }
}

Download this example.

Advertisement

Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: February 22, 2008

Ask Questions?    Discuss: Creating a Custom Formatter for a Logger Handler in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
dipalee
February 9, 2012
Map Classes and Interfaces

what is use of map classes and interfaces? where are use this map classes and interfaces? What is advantages of this map classes and interfaces?
DMCA.com