Şöyle yaparız
private static final Logger LOGGER = Logger.getLogger(JULExample.class.getName());
public static void main(String[] args) {
LOGGER.setUseParentHandlers(false); // Disable default console handler
ConsoleHandler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.INFO);
LOGGER.addHandler(consoleHandler);
try {
FileHandler fileHandler = new FileHandler("mylog.log", true);
fileHandler.setFormatter(new SimpleFormatter());
fileHandler.setLevel(Level.WARNING);
LOGGER.addHandler(fileHandler);
} catch (IOException e) {
LOGGER.severe("Failed to create log file: " + e.getMessage());
}
LOGGER.info("Starting application...");
try {
int result = 10 / 0;
} catch (ArithmeticException e) {
LOGGER.log(Level.SEVERE, "An error occurred", e);
}
LOGGER.warning("this is a warning message");
LOGGER.info("Application finished.");
}
Hiç yorum yok:
Yorum Gönder