Create a sequence of log files (original) (raw)

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

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.

Photo of Ilias Tsagklis

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

Back to top button