• 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)

Java Logging Handlers Example

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

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:

  • Basic Configuration using Log4j
  • Log4j 2 Tutorials
  • Log4j 2 + Spring MVC Integration

also read:

  • Basic Configuration using Log4j
  • Log4j 2 Tutorials
  • Log4j 2 + Spring MVC Integration

– See more at: https://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>

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 Logging Example (java.util.logging.Logger)
Next Post: java.util.Formatter Example »

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