Home Java Example Java Util Creating a Custom Formatter for a Logger Handler in Java



Creating a Custom Formatter for a Logger Handler in Java
Posted on: February 22, 2008 at 12:00 AM
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.

Related Tags for Creating a Custom Formatter for a Logger Handler in Java:
javacxmlfileideloggingormdateformscriptiotypescustompropertyuserformattypeusersipvicontentusingintappendthisidlogpackagewriteappsimplecreatedefinehandlehandlerforrecordaddprogramrecordstoramcilloggerciastwsdescriptioneilitlastformatterdessectionnotlsliusepeiminnomlrmaddinaddingasmheadnttroggddoggjpackadesthroughtrueendallagepenmemeanhowhrintoproppackcreatingdefinedscreatedeegincontentsatpackisheadinghallivlfmplpropereaandarrdsrtropesimvatttwxmscrssririprdthavstatiaphatfilehandlerfinpleplpropprndonomogro


More Tutorials from this section

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
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.