1

According to this example http://tutorials.jenkov.com/java-logging/logger-hierarchy.html

we have three loggers "com", "com.jenkov" and "com.jenkov.web". Why these has to be in hierarchy, what's the advantage of rolling up the messages ?

sai chandesh
  • 91
  • 1
  • 7

1 Answers1

0

Why these has to be in hierarchy...?

Inheritance. Per Java™ Logging Overview - 1.3 Loggers:

In particular, a logger may inherit:

Logging level. If a Logger's level is set to be null then the Logger will use an effective Level that will be obtained by walking up the parent tree and using the first non-null Level. Handlers. By default a Logger will log any output messages to its parent's handlers, and so on recursively up the tree. Resource bundle names. If a logger has a null resource bundle name, then it will inherit any resource bundle name defined for its parent, and so on recursively up the tree.

Using the package deceleration in Java which allows you to either listen, turn off, or sift logging for class, package, or even an organisational 3rd party lib. It also means you don't have to write a configuration file with hundreds of logger names to disable all logging. Setting the root adjusts all children for you in one line. Since you have a tree structure you can recursively apply that logic to the child loggers to disable a specific library or a logical program module.

In the tutorial it is explained:

Logger.getLogger("com.jenkov.web") has only one parent and that is the root logger. Looks like this: "" <- "com.jenkov.web"

However, Logger logger = Logger.getLogger(""); Logger logger1 = Logger.getLogger("com"); Logger logger2 = Logger.getLogger("com.jenkov"); Logger logger3 = Logger.getLogger("com.jenkov.web");

forces the LogManager to create a different hierarchy which looks like this: "" <- "com." <- "com.jenkov." <- "com.jenkov.web"

Adding code like this is not required in log4j but is required in JUL if you want to modify the hierarchy so you can control it at runtime. Also loggers should be pinned in memory to avoid garbage collection if you need to do this trick.

...what's the advantage of rolling up the messages ?

I assume you mean sending messages to parent loggers??? It enables the configuration to sift log records into specific Handlers and sharing common handler settings. For instance, in a development environment configuration you might want to send all output to the console by attaching to the root and then only send to the FileHandler for a specific package. The advantage with the logger tree is that you don't have to attach a second ConsoleHandler to the specific package to see console output.

jmehrens
  • 10,580
  • 1
  • 38
  • 47