Home » Core Java » util » logging » Create a sequence of log files

About Ilias Tsagklis

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

Create a sequence of log files

This is an example of how to create a sequence of log files. Creating a sequence of log files implies that you should:

  • Create a new instance of a Logger.
  • Create an instance of FileHandler with a specific number of logging files sequences, a specific size for each file and append set to true. In the example 5 files will be created, with size of each one set to 1024 bytes, with a specific pattern for file name and append set to true.
  • Set a Formatter to format LogRecords for this Handler, with the
    setFormatter(Formatter newFormatter) API method of the Handler.
  • Add the Handler to the Logger, with addHandler(Handler handler) API method of the Logger.
  • Configure the logger so as no to send its output to it’s parent Logger, with setUseParentHandlers(boolean useParentHandlers), and parameters set to false.
  • Log messages, using the API methods of Logger, such as info(String msg), warning(String msg). The logs will be forwarded to the File Handler, that will write the log messages to the files.

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

package com.javacodegeeks.snippets.core;

import java.util.logging.Logger;
import java.util.logging.FileHandler;
import java.util.logging.SimpleFormatter;
import java.io.IOException;
 
public class SequencedLogFile {
    
    public static final int FILE_SIZE = 1024;
 
    public static void main(String[] args) {

  Logger logger = Logger.getLogger(SequencedLogFile.class.getName());
 

  try {


// Create an instance of FileHandler with 5 logging files sequences.


FileHandler handler = new FileHandler("sample.log", FILE_SIZE, 5, true);


handler.setFormatter(new SimpleFormatter());


logger.addHandler(handler);


logger.setUseParentHandlers(false);

  } catch (IOException e) {


logger.warning("Failed to initialize logger handler.");

  }
 

  logger.info("Logging info message.");

  logger.warning("Logging warn message.");
    }
}

Output:

Αυγ 12, 2012 12:56:03 ΜΜ com.javacodegeeks.snippets.core.SequencedLogFile main
INFO: Logging info message.
Αυγ 12, 2012 12:56:03 ΜΜ com.javacodegeeks.snippets.core.SequencedLogFile main
WARNING: Logging warning message.

 
This was an example of how to create a sequence of log files 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