Java Logging: Logger Hierarchy
2015-06-16 00:48
666 查看
Table of Contents
Filters and Handlers in the Logger HierarchyLog Levels of Loggers in the Hierarchy
The
Logger's used in your application are typically organized into a hierarchy, as mentioned elsewhere in this tutorial. This text will take a closer look at how this
Loggerhierarchy works.
When you create a
Loggeryou pass a name to the
Logger.getLogger()method. Here is an example:
Logger logger = Logger.getLogger("com.jenkov.web");
In this example the name of the created
Loggeris
com.jenkov.web.
The name indicates a hierarchy of
Loggers. Each . (dot) in the name marks a level in the hierarchy. (Note: These levels are different from the log levels of the messages logged.). With the name
com.jenkov.webthe
Loggerhas 3 parents, with these names:
"" "com" "com.jenkov"
Here is a diagram illustrating the full hierarchy:
![]() |
Java Logger Hierarchy Example |
getParent()on the
Loggercreated in the example above, you will get the
Loggerwith the name
com.jenkov. If you call
getParent()on that
Loggeryou will get the
Loggerwith the name
com. The root of the hierarchy is the
Loggerwith the empty string as name (
"").
One thing to note is, that you need to create the
Loggersin the hierarchy, before they exist. So, if you do create a
Loggerlike this:
Logger logger = Logger.getLogger("com.jenkov.web");
... and call
getParent()method, you will get the
Loggerwith the name
"". The reason for this is, that none of the in-between
Logger's in hierarchy have been created. You need to do 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 call
getParent()on
logger3, you will get the
Loggerwith the name
com.jenkov. The parent of that
Loggeris named
cometc.
Filters and Handlers in the Logger Hierarchy
When a message is passed to aLogger, the message is passed through the
Logger's
Filter, if the
Loggerhas a
Filterset. The
Filtercan either accept or reject the message. If the message is accepted, the message is forwarded to the
Handler's set on the
Logger. If no
Filteris set, the message is always accepted.
If a message is accepted by the
Filter, the message is also forwarded to the
Handler's of the parent
Logger's. However, when a message is passed up the hierarchy, the message is not passed through the
Filter's of the parent
Logger's. The
Filter's are only asked to accept the message when the message is passed directly to the
Logger, not when the message comes from a child
Logger.
Here is a diagram illustrating the propagation of messages up the
Loggerhierarchy:
![]() |
Java Logger Hierarchy Example |
First, here is an example that creates 3 loggers in the hierarchy. A
ConsoleHandleris assigned to 2 of them. The root
Loggerhas a
Handlerby default, so it is not necessary to add a
Handlerto that. Then 3 messages are logged. One message via each
Loggerin the 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 log INFO: msg: 14-01-2012 10:32:42 logging.LoggingExamples main INFO: msg: 1 14-01-2012 10:32:42 logging.LoggingExamples main INFO: msg: 1 14-01-2012 10:32:42 logging.LoggingExamples main INFO: msg: 1.2 14-01-2012 10:32:42 logging.LoggingExamples main INFO: msg: 1.2 14-01-2012 10:32:42 logging.LoggingExamples main INFO: msg: 1.2
Notice how the first message is being logged only once, by the root
Logger.
The second message is being logged twice: Once by the
1
Logger, and once by the root
Logger.
The third message is being logged three times: Once by the
1.2
Logger, once by the
1
Logger, and once by the root
Logger.
Now, lets try adding a filter to the middle
Loggerin the hierarchy, the
Loggernamed
1. Here is 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
Filterrejecting all messages set on the middle
Logger, the output logged is this:
14-01-2012 11:33:21 java.util.logging.LogManager$RootLogger log INFO: msg: 14-01-2012 11:33:21 logging.LoggingExamples main INFO: msg: 1.2 14-01-2012 11:33:21 logging.LoggingExamples main INFO: msg: 1.2 14-01-2012 11:33:21 logging.LoggingExamples main INFO: msg: 1.2
Notice how the first message is still logged once, and the third message still logged three times, once by each
Loggerin the hierarchy.
The second message, however, the message sent to the middle
Loggeris not logged at all. The
Filterset on the middle
Loggerwhich always return false (meaning it never accepts any messages), filters out all messages logged via this
Logger. Thus, the second message is never logged, nor propagated up the
Loggerhierarchy.
Notice though, that the message propagated up the hierarchy from the
Loggernamed
1.2is still logged by the middle
Logger, and still forwarded up to the root
Logger. The
Filterset on the middle
Loggerdoes 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 be logged, separately for eachLogger. If a
Loggerhas a certain log level set, then all messages of less importance than the set log level is ignored. Additionally, all levels below the set log level are not propagated up the
Loggerhierarchy. That is a different from the behaviour of a
Filter.
Here is a code example that shows a
Loggerhierarchy with 3
Logger's in, and with the middle logger (named
1) having a minimum log level of
WARNINGset.
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 is, that no messages logged to the middle logger of less importance than
WARNINGis logged, nor propagated up the hierarchy. The log level
INFOis less important than
WARNING, so the
INFOmessage logged to the middle logger is ignored, and not propagated to the root logger.
Another peculiar result of the above code is, that the
INFOmessage passed to the bottom
Logger(named
1.2) is also ignored, and not propagated. The reason for this is, that the bottom
Loggerdoes not have a log level set, and thus inherits the level set on its parent in the
Loggerhierarchy. In other words, the bottom
Loggerinherits the log level set on the middle
Logger.
The code example above is illustrated in this diagram:
![]() |
Example of how log levels work in the Logger hierarchy. |
14-01-2012 13:25:32 java.util.logging.LogManager$RootLogger log INFO: msg:
Only the message logged directly via the root
Loggeris actually logged.
In order to enable all
INFOmessages to be logged from the bottom
Logger(named
1.2), even if the middle
Loggerhas a log level of
WARNING, 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 is that the
INFOmessage logged on the bottom
Logger(named
1.2) is now logged, but it is still not propagated up the hierarchy. Well, it is, but the middle
Loggerfilters it out, because the middle
Loggerhas a log level of
WARNINGset. Thus, the message is not logged by the middle
Loggernor propagated up the hierarchy.
The code example above is illustrated in this diagram:
![]() |
Example of how log levels work in the Logger hierarchy. |
Loggersymbolizes, that only messages of
WARNINGor higher importance are propagated up the hierarchy.
Here is the output logged by the above code:
14-01-2012 13:30:27 java.util.logging.LogManager$RootLogger log INFO: msg: 14-01-2012 13:30:27 logging.LoggingExamples main INFO: msg: 1.2
The
INFOmessage logged to the bottom
Loggeris logged, but not propagated.
The
INFOmessage logged to the middle
Loggeris neither logged, nor propagated.
The
INFOmessage logged to the root
Loggeris logged.
The specific log levels are covered in more detail in the text on Log Levels. The coverage here only serves to explain how the log level affects message propagation in the
Loggerhierarchy.
相关文章推荐
- Java Logging: Logger
- java笔记15 多线程2(线程通信、Lock)
- java使用dom4j解析xml字符串
- Java Logging: Basic Usage
- myeclipse svn插件安装
- 【Java8】Java 8十个lambda表达式案例
- [Java 实现AES加密解密]
- java中的深复制和浅复制
- Java基础之数组
- 【Java8】JDK8.0新特性—Lamda表达式
- java中替换字符以及回车换行
- Java Mail发送
- java.sql.SQLException: ORA-01000: 超出打开游标的最大数
- maven项目和eclipse项目之间的转换
- Java并发编程-09-使用读写锁同步数据访问
- Java Logging: Overview
- java学习
- JDK之List分析
- java 框架基础知识(7)----事务基础知识-->Spring事务管理
- Java使用SOAP获取webservice实例解析