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.


 

Ads

Tutorials   
How to read properties file in Java? Generate random numbers in Java Cloneable Interface in Java What is the flaw with the Stack class? List In Java Java Stack Example HashSet In Java Enumeration in java How to generate random number in java Iterator in java Queue in java Transient Java Keyword clone method in Java Java Queue example Logger in Java Java current date - How to get current date in Java Collection : LinkedList Example Collection : ArrayList Example Random alpha numeric string of fixed size Java List Iterator Example The Hashtable Class The BitSet class Java Vector Java Enumeration Data Structures in Java Java Transient Variable Java Date Format Example Java Properties File Example Map.Entry Java Interface Java Property File java preferences,Change the value of Preferences by user Retrieving a Preference Node Java hasNext Write Keys and Values to the Properties file in Java Get All Keys and Values of the Properties files in Java Properties file in Java Properties file in Java Determining If a Preference Node Contains a Specific Value Change the value of Preferences by user Clone method example in Java Implementing a Simple Event Notifier Determining When a Preference Node Is Added or Removed Listening for Changes to Preference Values in a Preference Node Importing Preferences Finding a Preference in a Preference Tree Retrieving the Parent and Child Nodes of a Preference Node Determining If a Preference Node Exists Removing a Preference Node Creating a Preference Node Getting the Maximum Size of a Preference Key and Value
Ads

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.

Advertisements

Ads
Share on Google+Share on Google+

Creating a Custom Formatter for a Logger Handler in Java

Posted on: February 22, 2008 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Advertisements

 

Discuss: Creating a Custom Formatter for a Logger Handler in Java  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
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?

Ads

 

Ads