• Menu
  • Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

JavaBeat

Java Tutorial Blog

  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)
  • Java
    • Java 7
    • Java 8
    • Java EE
    • Servlets
  • Spring Framework
    • Spring Tutorials
    • Spring 4 Tutorials
    • Spring Boot
  • JSF Tutorials
  • Most Popular
    • Binary Search Tree Traversal
    • Spring Batch Tutorial
    • AngularJS + Spring MVC
    • Spring Data JPA Tutorial
    • Packaging and Deploying Node.js
  • About Us
    • Join Us (JBC)

Create Custom Formatter for Logger Handler

June 23, 2014 //  by Krishna Srinivasan//  Leave a Comment

This example shows how to write a simple custom Formatter for the logging in Java. Java provides java.util.Formatter class for extending the format of the messages and that can be applied to the handlers. In our previous example, I have explained how to write the logging handlers.

If you want to write the custom formatters, you have to extend the java.util.Formatter and override the format(LogRecord) with your own implementation. If you look at the example, you can use your own text before and after of the log messages. This formatter will be invoked everytime log messages are printed.

also read:

  • Compress Files To ZIP Format Using Java
  • Compress and Uncompress a Zip File
  • Locking files using Java

Lets look at the example.

JavaLoggerFormatterExample.java

package javabeat.net.java.core;

import java.io.IOException;
import java.util.logging.FileHandler;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 * Simple Java Logger Handler example
 *
 * @author Krishna
 *
 */
public class JavaLoggerFormatterExample {
	private static final Logger LOGGER = Logger.getLogger(JavaLoggerFormatterExample.class.getName());
	public static void main(String[] args) throws SecurityException, IOException {
		String str = null;

		//Creating the log handlers
		FileHandler fileHandler = new FileHandler("javabeat.log");
		fileHandler.setFormatter(new CustomFormatter());

		//Add handlers to the logger
		LOGGER.addHandler(fileHandler);

		//set the levels for each handler
		fileHandler.setLevel(Level.ALL);
		LOGGER.setLevel(Level.ALL);

		LOGGER.info("Logger Name: "+LOGGER.getName());
		LOGGER.warning("Can cause NullPointerException");
		try{
			System.out.println(str.toString());
		}catch(NullPointerException ex){
			LOGGER.log(Level.SEVERE, "Exception occur", ex);
		}
	}
}

CustomFormatter.java

package javabeat.net.java.core;

import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

/**
 * Example for Custom Formatter
 *
 * @author Krishna
 *
 */
public class CustomFormatter extends Formatter{
	@Override
	public String format(LogRecord record) {
		StringBuffer sb = new StringBuffer();
		sb.append("\nLogged message on Date "+new Date().getDate() +" : ");
		sb.append(record.getMessage());
		return sb.toString();
	}
}

Output…


Logged message on Date 23 : Logger Name: javabeat.net.java.core.JavaLoggerFormatterExample
Logged message on Date 23 : Can cause NullPointerException
Logged message on Date 23 : Exception occur

Category: JavaTag: Java Logging, Java Util

About Krishna Srinivasan

He is Founder and Chief Editor of JavaBeat. He has more than 8+ years of experience on developing Web applications. He writes about Spring, DOJO, JSF, Hibernate and many other emerging technologies in this blog.

Previous Post: « java.util.Formatter Example
Next Post: Bootstrap Input Groups »

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

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

Primary Sidebar

Follow Us

  • Facebook
  • Pinterest

FEATURED TUTORIALS

New Features in Spring Boot 1.4

Difference Between @RequestParam and @PathVariable in Spring MVC

What is new in Java 6.0 Collections API?

The Java 6.0 Compiler API

Introductiion to Jakarta Struts

What’s new in Struts 2.0? – Struts 2.0 Framework

JavaBeat

Copyright © by JavaBeat · All rights reserved
Privacy Policy | Contact