6

In the Java class java.util.logging.Logger, what is the difference between the global and the root loggers? Are they the same? The Logger::getGlobal method returns the global logger. And calling Logger::getParent repeatedly until the result is null will yield the root logger. When I call Logger::getLogger with an empty string for the name argument, does this return the global logger or the root logger?

http://docs.oracle.com/javase/9/docs/api/java/util/logging/Logger.html

Brian Schack
  • 300
  • 3
  • 14
  • 1
    see https://stackoverflow.com/questions/19942521/in-java-util-logging-what-is-the-global-logger-for for the global logger. Thus, the root logger and the global logger should not be the same. – lwi Mar 08 '18 at 21:20

1 Answers1

7

In the Java class java.util.logging.Logger, what is the difference between the global and the root loggers? Are they the same?

No they are not the same.

public static void main(String[] args) throws Exception {
    System.out.println(Logger.getLogger(""));
    System.out.println(Logger.getGlobal());
    System.out.println(Logger.getGlobal().getParent());
}

Will for example output:

java.util.logging.LogManager$RootLogger@27082746
java.util.logging.Logger@66133adc
java.util.logging.LogManager$RootLogger@27082746

As you can see, the root logger is the parent of the global logger. The root logger is used to propagate levels to child loggers and is used hold the handlers that can capture all published log records. The global logger is just a named logger that has been reserved for causal use. It is the System.out of the logging framework, so use it only for throw away code.

Logger::getLogger with an empty string for the name argument, does this return the global logger or the root logger?

It returns the root logger.

jmehrens
  • 10,580
  • 1
  • 38
  • 47