Java Programing laungage

java.util Projects

java.util Project 1

Creating Custom Formatter

In This page of the programming tutorials we are going to create the a custom formatter for a logger handler. Java provides two types formatter SimpleFormatter and XMLFormatter. But, in the java logging package allows for creating a custom formatter through the logger handler. this means that the formatter is created by the user. A logger handler writes log records to another file by using the logger formatter.

Previous Home Next

Program Description

In this Example we are create to the 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)

It is a method of Formatter class which is used to return string types formatter records. This method takes a argument which is any type of handler.


package r4r;
import java.util.*;
import java.util.logging.FileHandler;
import java.util.logging.Handler;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import java.util.logging.XMLFormatter;
public class customformattest extends XMLFormatter  {
public static void main(String[] args) {
Logger logg = Logger.getLogger("R4RTechSoft Solution");

FileHandler fhand = new FileHandler("Sam.html", true);
fhand.setFormatter(new customformattest());
logg.addHandler(fhand);"This Custom formatter is created by R4RTechSoft Solution");
catch (IOException e){}
public String format(LogRecord rec) {
StringBuffer sb = new StringBuffer(1000);
return sb.toString();
		 public String getHead(Handler h) {
return "<HTML><HEAD><h1>"+(new Date())+"</h1></HEAD><BODY>\n";

Previous Home Next