Home » Core Java » util » logging » Create custom Formatter for Logger handler

About Byron Kiourtzoglou

Byron Kiourtzoglou
Byron is a master software engineer working in the IT and Telecom domains. He is an applications developer in a wide variety of applications/services. He is currently acting as the team leader and technical architect for a proprietary service creation and integration platform for both the IT and Telecom industries in addition to a in-house big data real-time analytics solution. He is always fascinated by SOA, middleware services and mobile development. Byron is co-founder and Executive Editor at Java Code Geeks.

Create custom Formatter for Logger handler

In this example we shall show you how to create custom Formatter for a Logger‘s Handler. The Handler usually uses a Formatter associated with it to format LogRecords. The Formatter takes a LogRecord and converts it to a string. LogRecord objects are used to pass logging requests between the logging framework and individual log Handlers. To create a custom Formatter to format LogRecords one should perform the following steps:

  • Create a new CustomFormatter class that extends the Formatter and overrides its format(LogRecord record) method. In the example, the customFormatter uses a StringBuffer to append a prefix and a suffix to the record, using the append(String str) API method of the StringBuffer and returns a String representation of the LogRecord.
  • Create a new Logger instance.
  • Create a FileHandler for the Logger to write to a specific file, and add it to the Logger with addHandler(Handler handler) API method.
  • Set the custom formatter to the handler with setFormatter(Formatter newFormatter) API method, to format its records,

as described in the code snippet below.

package com.javacodegeeks.snippets.core;

import java.util.logging.FileHandler;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.logging.Logger;

public class CreateCustomFormatterForLoggerHandler {
	
	public static void main(String[] args) throws Exception {
		
		boolean append = false;
	    FileHandler handler = new FileHandler("default.log", append);

	    Logger logger = Logger.getLogger("com.javacodegeeks.snippets.core");
	    logger.addHandler(handler);

	    handler.setFormatter(new MyCustomFormatter());
	    logger.info("custom formatter - info message");
		
	}
	
	private static class MyCustomFormatter extends Formatter {

		@Override
		public String format(LogRecord record) {
			StringBuffer sb = new StringBuffer();
			sb.append("Prefixn");
			sb.append(record.getMessage());
			sb.append("Suffixn");
			sb.append("n");
			return sb.toString();
		}
		
	}

}

Output:

Nov 19, 2011 4:11:51 PM com.javacodegeeks.snippets.core.CreateCustomFormatterForLoggerHandler main
INFO: custom formatter - info message

default.log

Prefix
custom formatter - info message
Suffix

 
This was an example of how to create custom Formatter for Logger handler in Java.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area from our partners over at ZipRecruiter

 

Want to take your Java skills to the next level?

Grab our programming books for FREE!

Here are some of the eBooks you will get:

  • Spring Interview QnA
  • Multithreading & Concurrency QnA
  • JPA Minibook
  • JVM Troubleshooting Guide
  • Advanced Java
  • Java Interview QnA
  • Java Design Patterns