Java Logging Handlers Example

In our previous example I have explained how to write a simple logging configuration in your Java program. This example shows how to use the different log handlers for printing the log messages. Log handlers are the medium where the log messages has to be transfered for the future reference. In bigger applications, the handlers can be File, Database or even remote systems. In this example, I have used ConsoleHandler and FileHandler for the simplicity. The following are the list of log handlers defined in the Java packages.

  1. ConsoleHandler
  2. FileHandler
  3. StreamHandler
  4. SocketHandler
  5. MemoryHandler

Lets look at the simple example which prints the messages from the Java application.

also read:

also read:

- See more at: http://www.javabeat.net/java-util-logging-logger/#sthash.wkJPvdhq.dpuf

JavaLoggerHandlerExample.java

package javabeat.net.java.core;

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

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

		//Creating the log handlers
		ConsoleHandler consoleHandler = new ConsoleHandler();
		FileHandler fileHandler = new FileHandler("javabeat.log");

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

		//set the levels for each handler
		consoleHandler.setLevel(Level.ALL);
		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);
		}
	}
}

If you run the above example, you would have a new file (javabeat.log) created under the root of the project folder. The contents of the file looks like this:

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2014-06-23T14:04:55</date>
  <millis>1403512495462</millis>
  <sequence>0</sequence>
  <logger>javabeat.net.java.core.JavaLoggerHandlerExample</logger>
  <level>INFO</level>
  <class>javabeat.net.java.core.JavaLoggerHandlerExample</class>
  <method>main</method>
  <thread>1</thread>
  <message>Logger Name: javabeat.net.java.core.JavaLoggerHandlerExample</message>
</record>
<record>
  <date>2014-06-23T14:04:55</date>
  <millis>1403512495526</millis>
  <sequence>1</sequence>
  <logger>javabeat.net.java.core.JavaLoggerHandlerExample</logger>
  <level>WARNING</level>
  <class>javabeat.net.java.core.JavaLoggerHandlerExample</class>
  <method>main</method>
  <thread>1</thread>
  <message>Can cause NullPointerException</message>
</record>
<record>
  <date>2014-06-23T14:04:55</date>
  <millis>1403512495528</millis>
  <sequence>2</sequence>
  <logger>javabeat.net.java.core.JavaLoggerHandlerExample</logger>
  <level>SEVERE</level>
  <class>javabeat.net.java.core.JavaLoggerHandlerExample</class>
  <method>main</method>
  <thread>1</thread>
  <message>Exception occur</message>
  <exception>
    <message>java.lang.NullPointerException</message>
    <frame>
      <class>javabeat.net.java.core.JavaLoggerHandlerExample</class>
      <method>main</method>
      <line>36</line>
    </frame>
  </exception>
</record>
</log>

Comments

comments

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.

Speak Your Mind

*