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
Normally, when enabling logging in an application, you define more granular loggers, usually per Java packages or classes. If you do not want to do that, you can use this global logger, which will handle all logging statements, no matter the library, package or class they are contained in.
The rootlogger is always the logger configured in the log4j. properties file, so every child logger used in the application inherits the configuration of the rootlogger . The logging levels are (from smaller to greater) : ALL, DEBUG, INFO, WARN, ERROR, FATAL, OFF .
One of the most popular solutions for the Java world is the Apache Log4j 2 framework. Maintained by the Apache Foundation, Log4j 2 is an improvement on the original Log4j, which was the most popular logging framework in Java for many years.
This article will help you choose a logging API by evaluating two of the most widely used Java logging libraries: the Apache Group's Log4j and the java. util. logging package (referred to as "JUL").
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With