3

I set logging level to CONFIG, but don't see messages written on CONFIG level. What I am missing?

Configuration:

Logger logger = java.util.logging.Logger.getLogger("xxx");
logger.setLevel(java.util.logging.Level.CONFIG);

Tests:

logger.log(java.util.logging.Level.SEVERE, "severe");
logger.log(java.util.logging.Level.WARNING, "warning");
logger.log(java.util.logging.Level.INFO, "info");
logger.log(java.util.logging.Level.CONFIG, "config");
logger.log(java.util.logging.Level.FINE, "fine");
logger.log(java.util.logging.Level.FINER, "finer");
logger.log(java.util.logging.Level.FINEST, "finest");

Output:

SEVERE: severe
WARNING: warning
INFO: info
4

2 回答 2

8

我通常使用logback来实现日志记录,这似乎有更好的记录。所以我建议切换到那个。

但是要回答您的问题,我认为正在发生的事情是您Logger的配置正确,但Handler它发送的消息却不是。默认配置可能会将具有INFO级别日志记录的处理程序附加到根记录器。

编辑:我写了一个小测试程序来验证,你确实需要在附加到根记录器的处理程序上设置级别。你可以这样做:

for (Handler handler : Logger.getLogger("").getHandlers()) {
    handler.setLevel(Level.CONFIG);
}
logger.config("config");

给出作为输出:

2011 年 2 月 11 日下午 4:32:14 测试主
配置:配置

这将设置附加到此的所有处理程序的级别。显然,更好的选择是编写您自己的选项文件并显式配置您的记录器。一个快速的谷歌出现了这篇关于这个主题的文章。

您还可以尝试使用类路径上的属性文件进行配置,该文件内容为:

java.util.logging.ConsoleHandler.level=CONFIG
于 2011-02-11T15:29:16.883 回答
1

This is the expected behavior.

When you define a level of logs, you will see all the logs of this level, but also the ones linked to higher level.

The order is the following:

  • SEVERE (highest value)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST (lowest value)

So in your case, if you define the level to INFO, you will only see SEVERE, WARNING and INFO logs, and not CONFIG messages.


Edit, to answer your corrected question:

Maybe a third party library is used for your Logger class (log4j, slf4j, and so on), and this library defines its own level of log. For example, for log4j, there are only the following levels:

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

In such case, the level CONFIG is considered as a INFO level, that explains your current behavior.

于 2011-02-11T15:06:02.217 回答