Skip to content

Instantly share code, notes, and snippets.

@rupert-ong
Last active September 16, 2018 01:54
Show Gist options
  • Save rupert-ong/9cfe11114ac0457b6802f1c10378dc45 to your computer and use it in GitHub Desktop.
Save rupert-ong/9cfe11114ac0457b6802f1c10378dc45 to your computer and use it in GitHub Desktop.
Java: Logging Hierarchy #java #logging #hierarchy
# Specify -Djava.util.logging.config.file=log.properties in the VM arguments
# Define parent logger
com.ps.handlers=java.util.logging.ConsoleHandler
com.ps.level=INFO
# Define child logger, its handler and formatter
java.util.loggging.FileHandler.level=ALL
java.util.logging.FileHandler.pattern=./main_%g.log
java.util.logging.SimpleFormatter.format=%1$tc %2$s%n%4$s: %5$s%6$s%n
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter
com.ps.Main.handlers=java.util.logging.FileHandler
com.ps.Main.level=ALL
package com.ps;
import java.util.logging.Level;
import java.util.logging.Logger;
public class Main {
// Parent and child logger
static Logger pkgLogger = Logger.getLogger("com.ps");
static Logger logger = Logger.getLogger("com.ps.Main");
public static void main(String[] args) {
logger.entering("com.ps", "Main"); // Logs to com.ps.Main (Finer level)
logger.log(Level.INFO, "We are logging"); // Logs to both loggers
logger.exiting("com.ps", "Main"); // Logs to com.ps.Main (Finer level)
}
}
@rupert-ong
Copy link
Author

Handlers specify the output of our log (ConsoleHandler, FileHandler), and Formatters alter the format.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment