In this example you learn how to limit the size of a log file when using a FileHandler
handler. Limiting the log file will prevent the log file to grow wildly without limit.
package org.kodejava.example.util.logging;
import java.util.logging.Logger;
import java.util.logging.FileHandler;
import java.io.IOException;
public class LogFileLimit {
// The log file size is set to 1MB.
public static final int FILE_SIZE = 1024 * 1024;
public static void main(String[] args) {
Logger logger = Logger.getLogger(LogFileLimit.class.getName());
try {
// Create a FileHandler with 1MB file size and a single log file. We
// also tell the handler to append the log message.
FileHandler handler = new FileHandler("myapp.log", FILE_SIZE, 1, true);
logger.addHandler(handler);
} catch (IOException e) {
logger.warning("Failed to initialize logger handler.");
}
logger.info("Test info");
logger.warning("Test warning");
logger.severe("Test severe");
}
}
Wayan Saryada
Founder at Kode Java Org
I am a programmer, a runner, a recreational diver, currently live in the island of Bali, Indonesia. Mostly programming in Java, Spring Framework, Hibernate / JPA. If these posts help, you can support me, buy me a cup of coffee or tea. Thank you 🥳
Latest posts by Wayan Saryada (see all)
- How do I set the time of java.util.Date instance to 00:00:00? - October 24, 2019
- How to Install Consolas Font in Mac OS X? - March 29, 2019
- How do I clear the current command line in terminal? - February 14, 2019