我正在学习 java.util.Logging API,但我不明白为什么它会产生冗余输出,如下面的示例代码所示:
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");
控制台的输出:
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
为什么将日志消息传递给父级,并且多次显示相同的消息!?这完全没有效率,而且对于重复信息,日志文件会增加得太快。有谁知道为什么?这就是 Logging 应该具备的性质吗?
注意:示例代码复制自http://tutorials.jenkov.com/java-logging/logger-hierarchy.html