Java Logging:logger Hierarchy

Source: Internet
Author: User
Tags dashed line

Table of Contents
    • Filters and handlers in the Logger Hierarchy
    • Log levels of loggers in the Hierarchy

Loggerthe ' s used in your application is typically organized into a hierarchy, as mentioned elsewhere in this tutorial. This text would take a closer look at how this Logger hierarchy works.

When you create a, you Logger pass a name to the Logger.getLogger() method. Here are an example:

Logger Logger  = Logger.getlogger ("Com.jenkov.web");

In this example the name of the created Logger is com.jenkov.web .

The name indicates a hierarchy of Loggers . Each. (dot) in the name marks A, level in the hierarchy. (Note:these levels is different from the log levels of the messages logged.). com.jenkov.web Logger with the name the have 3 parents, with these names:

"" "com" "Com.jenkov"

Here are a diagram illustrating the full hierarchy:

Java Logger Hierarchy Example

If you call getParent() the ' on Logger the ' created in the ' example above, you'll get the with the Logger name com.jenkov . If you are on this you'll get the with the getParent() Logger Logger name com . The root of the hierarchy is the and the Logger empty string as name ( "" ).

One thing to note are, that's need to create Loggers the in the hierarchy, before they exist. So, if you do create a like this Logger :

Logger Logger  = Logger.getlogger ("Com.jenkov.web");

... getParent() and call method, you'll get the with the Logger name "" . The reason for this is, and that's none of the in-between Logger ' s in hierarchy has been created. You need to does the following to instantiate all the Logger ' s in the hierarchy:

Logger Logger  = Logger.getlogger (""); Logger logger1 = Logger.getlogger ("com"); Logger logger2 = Logger.getlogger ("Com.jenkov"); Logger Logger3 = Logger.getlogger ("Com.jenkov.web");

Now, if you are on, you'll getParent() logger3 get the With the Logger name com.jenkov . The parent of the is Logger named com etc.

Filters and handlers in the Logger Hierarchy

When a message was passed to a  Logger , the message is passed through the  Logger ' S&NB Sp filter , if the  Logger has a  filter  set. the  Filter  can either accept or reject the message. IF the message is accepted, the message was forwarded to THE  Handler 's set on the  Logger. If no  Filter  is Set, the message is always accepted.

If a message Filter is accepted by the, the message was also forwarded to the Handler ' s of the parent Logger '. However, when a message was passed up the hierarchy, the message was not passed through the Filter ' s of the parent ' s. Filterthe ' was only asked to accept the message when the message was passed directly Logger to the and not when the message Co Mes from a child Logger .

Here are a diagram illustrating the propagation of messages up the Logger hierarchy:

Java Logger Hierarchy Example

To show effect I'll illustrate it using a few code examples.

First, here's an example-creates 3 loggers in the hierarchy. A is ConsoleHandler assigned to 2 of them. The root Logger has a Handler by default, so it's not necessary to add a to that Handler . Then 3 messages is logged. One message via each in the Logger hierarchy. Here is the code:

Logger Logger      = Logger.getlogger (""); Logger logger1     = Logger.getlogger ("1"); Logger logger1_2   = Logger.getlogger ("1.2"); Logger1    . AddHandler (New Consolehandler ()); Logger1_2  . AddHandler (New Consolehandler ()), logger     . Info ("msg:"), Logger1    . info ("Msg:1"), logger1_2  . info (" msg:1.2 ");

The output in the log (console) of this code is:

14-01-2012 10:32:41 java.util.logging.logmanager$rootlogger loginfo:msg:14-01-2012 10:32:42 logging. Loggingexamples maininfo:msg:114-01-2012 10:32:42 logging. Loggingexamples maininfo:msg:114-01-2012 10:32:42 logging. Loggingexamples maininfo:msg:1.214-01-2012 10:32:42 logging. Loggingexamples maininfo:msg:1.214-01-2012 10:32:42 logging. Loggingexamples maininfo:msg:1.2

Notice How the first message was being logged only once, by the root Logger .

The second message is being logged twice:once 1 Logger by the root Logger .

The third message is being logged three times:once by 1.2 Logger the, Once 1 Logger by the, and Once by the root .

Now, lets try adding a filter Logger to the middle in the hierarchy, the Logger named 1 . Here are the code with the added Filter :

Logger Logger      = Logger.getlogger (""); Logger logger1     = Logger.getlogger ("1"); Logger logger1_2   = Logger.getlogger ("1.2"); Logger1.addhandler  (New Consolehandler ()); Logger1_2.addhandler (New Consolehandler ()); Logger1.setfilter (New Filter () {public    Boolean isloggable (LogRecord record) {    return false;    }    }); logger     . Info ("msg:"); Logger1    . Info ("Msg:1"); logger1_2  . Info ("msg:1.2");

With a Filter rejecting all messages set on the middle Logger , the output logged are this:

14-01-2012 11:33:21 java.util.logging.logmanager$rootlogger loginfo:msg:14-01-2012 11:33:21 logging. Loggingexamples maininfo:msg:1.214-01-2012 11:33:21 logging. Loggingexamples maininfo:msg:1.214-01-2012 11:33:21 logging. Loggingexamples maininfo:msg:1.2

Notice How the first message was still logged once, and the third message still logged three times, once by all in Logger th E hierarchy.

The second message, however, the message sent to the middle are not logged at all Logger . Filterthe set on the middle Logger which always return false (meaning it never accepts any messages), filters off all Messa GES logged via this Logger . Thus, the second message is never logged and nor propagated up the Logger hierarchy.

Notice though, that's the message propagated up the hierarchy from the Logger named are 1.2 still logged Logger by the middle , and still forwarded up to the root Logger . The Filter set on the middle Logger does is not touch propagated messages.

Log levels of loggers in the Hierarchy

As mentioned elsewhere in this tutorial you can set the log levels of messages to being logged, separately for each Logger . If a Logger has a certain log level set and then all messages of less importance than the Set log level is ignored. Additionally, all levels below the set log level is not propagated up the Logger hierarchy. That's a different from the behaviour of a Filter .

Here's a code example that shows a Logger hierarchy with 3 's Logger in, and with the middle logger (named 1 ) have a M Inimum log level of WARNING set.

Logger Logger      = Logger.getlogger (""); Logger logger1     = Logger.getlogger ("1"); Logger logger1_2   = Logger.getlogger ("1.2");  Logger1  . SetLevel (level.warning); Logger     . Info ("msg:"); Logger1    . Info ("Msg:1"); logger1_2  . Info ("msg:1.2");

The result of this was, that no messages logged to the middle logger of less importance than WARNING are logged, nor Propagat ed up the hierarchy. The log level INFO was less important than WARNING , so the INFO message logged to the middle logger are ignored, and not Propagated to the root logger.

Another peculiar result of the above code is, which the INFO message passed to the bottom Logger (named 1.2 ) is also Igno Red, and not propagated. The reason for this are, that of the bottom does not having Logger a log level set, and thus inherits the level set in its parent In the Logger hierarchy. In other words, the bottom inherits the log level is set on the Logger middle Logger .

The code example above is illustrated in this diagram:

Example of how log levels work in the Logger hierarchy.

Here are the output logged from the above code:

14-01-2012 13:25:32 Java.util.logging.logmanager$rootlogger loginfo:msg:

The message logged directly via the root is Logger actually logged.

In order to enable all INFO messages to is logged from the bottom Logger (named 1.2 ), even if the middle have Logger a log Level WARNING of, we add the following to the code (in bold):

Logger Logger      = Logger.getlogger (""); Logger logger1     = Logger.getlogger ("1"); Logger logger1_2   = Logger.getlogger ("1.2"); Logger1  . SetLevel (level.warning);  Logger1_2.setlevel (level.info); Logger     . Info ("msg:"); Logger1    . Info ("Msg:1"); logger1_2  . Info ("msg:1.2");

The result of this code was that the INFO message logged on the bottom Logger (named 1.2 ) was now logged, but it was still Not propagated up the hierarchy. Well, it's, but the middle Logger filters it out, because the middle have Logger a log level of WARNING set. Thus, the message is not logged by the middle Logger nor propagated up the hierarchy.

The code example above is illustrated in this diagram:

Example of how log levels work in the Logger hierarchy.

The dashed line between Logger the bottom and the middle symbolizes, which only messages of WARNING or higher importance is P Ropagated up the hierarchy.

Here are the output logged by the above code:

14-01-2012 13:30:27 java.util.logging.logmanager$rootlogger loginfo:msg:14-01-2012 13:30:27 logging. Loggingexamples maininfo:msg:1.2

INFOthe message logged Logger to the bottom are logged, but not propagated.

INFOthe message logged Logger to the middle is neither logged, nor propagated.

The INFO message logged to the root is Logger logged.

The specific log levels is covered in + detail in the text on log levels. The coverage here is serves to explain what the log level affects a message propagation in the Logger hierarchy.

Java Logging:logger Hierarchy

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.