Home » Core Java » util » logging » Set Formatter for Logger handler

About Ilias Tsagklis

Ilias Tsagklis
Ilias is a software developer turned online entrepreneur. He is co-founder and Executive Editor at Java Code Geeks.

Set Formatter for Logger handler

With this example we are going to demonstrate how to set a Formatter for a Logger Handler. The Formatter is used by the Handler to format LogRecords. In short, to set a Formatter for a Logger’s handler you should:

  • Create a new Logger instance with getLogger(String name) API method of Logger.
  • Create a new FileHandler to write to a specific file.
  • Add the handler to the Logger, with addHandler(Handler handler) API method of Logger.
  • Set a Formatter to the Handler, with setFormatter(Formatter newFormatter) API method. In the example we use the SimpleFormatter and the XMLFormatter.

Let’s take a look at the code snippet that follows:

package com.javacodegeeks.snippets.core;

import java.util.logging.FileHandler;
import java.util.logging.Logger;
import java.util.logging.SimpleFormatter;
import java.util.logging.XMLFormatter;

public class SetFormatterForLoggerHandler {
	
	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 SimpleFormatter());
	    logger.info("simple formatter - info message");

	    handler.setFormatter(new XMLFormatter());
	    logger.info("xml formatter - info message");
		
	}

}

Output:

Nov 19, 2011 4:03:15 PM com.javacodegeeks.snippets.core.SetFormatterForLoggerHandler main
INFO: simple formatter - info message
Nov 19, 2011 4:03:15 PM com.javacodegeeks.snippets.core.SetFormatterForLoggerHandler main
INFO: xml formatter - info message

default.log

Nov 19, 2011 4:03:15 PM com.javacodegeeks.snippets.core.SetFormatterForLoggerHandler main
INFO: simple formatter - info message
<record>
  <date>2011-11-19T16:03:15</date>
  <millis>1321711395420</millis>
  <sequence>1</sequence>
  <logger>com.javacodegeeks.snippets.core</logger>
  <level>INFO</level>
  <class>com.javacodegeeks.snippets.core.SetFormatterForLoggerHandler</class>
  <method>main</method>
  <thread>10</thread>
  <message>xml formatter - info message</message>
</record>
</log>

 
This was an example of how to set a Formatter for a 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