Home » Core Java » util » logging » Write Log entries to log file

About Byron Kiourtzoglou

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.

Write Log entries to log file

With this example we are going to demonstrate how to write Log entries to a log file. In short, to write Log entries to a log file you should:

  • Create a new FileHandler to write to a specific file.
  • Create a new Logger instance with getLogger(String name) API method of Logger.
  • Add the handler to the Logger, with addHandler(Handler handler) API method of Logger.
  • Invoke log methods of Logger to log messages in different levels, such as warning(String msg), info(String msg) and config(String msg) API methods. All logs will be written to the specified file by the FileHandler.

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;

public class WriteLogEntriesToLogFile {
	
	public static void main(String[] args) throws Exception {
		
		boolean append = true;
	    FileHandler handler = new FileHandler("default.log", append);

	    Logger logger = Logger.getLogger("com.javacodegeeks.snippets.core");
	    logger.addHandler(handler);
	    
	    logger.severe("severe message");

  logger.warning("warning message");

  logger.info("info message");

  logger.config("config message");

  logger.fine("fine message");

  logger.finer("finer message");

  logger.finest("finest message");
		
	}

}

Output:

Nov 19, 2011 3:40:55 PM com.javacodegeeks.snippets.core.WriteLogEntriesToLogFile main
SEVERE: severe message
Nov 19, 2011 3:40:55 PM com.javacodegeeks.snippets.core.WriteLogEntriesToLogFile main
WARNING: warning message
Nov 19, 2011 3:40:55 PM com.javacodegeeks.snippets.core.WriteLogEntriesToLogFile main
INFO: info message

default.log

<?xml version="1.0" encoding="windows-1252" standalone="no"?>
<!DOCTYPE log SYSTEM "logger.dtd">
<log>
<record>
  <date>2011-11-19T15:40:55</date>
  <millis>1321710055254</millis>
  <sequence>0</sequence>
  <logger>com.javacodegeeks.snippets.core</logger>
  <level>SEVERE</level>
  <class>com.javacodegeeks.snippets.core.WriteLogEntriesToLogFile</class>
  <method>main</method>
  <thread>10</thread>
  <message>severe message</message>
</record>
<record>
  <date>2011-11-19T15:40:55</date>
  <millis>1321710055322</millis>
  <sequence>1</sequence>
  <logger>com.javacodegeeks.snippets.core</logger>
  <level>WARNING</level>
  <class>com.javacodegeeks.snippets.core.WriteLogEntriesToLogFile</class>
  <method>main</method>
  <thread>10</thread>
  <message>warning message</message>
</record>
<record>
  <date>2011-11-19T15:40:55</date>
  <millis>1321710055323</millis>
  <sequence>2</sequence>
  <logger>com.javacodegeeks.snippets.core</logger>
  <level>INFO</level>
  <class>com.javacodegeeks.snippets.core.WriteLogEntriesToLogFile</class>
  <method>main</method>
  <thread>10</thread>
  <message>info message</message>
</record>
</log>

 
This was an example of how to write Log entries to a log file in Java.

(No Ratings Yet)
Start the discussion Views Tweet it!

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

 

Leave a Reply

avatar
  Subscribe  
Notify of