logging

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.

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.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button