Java 日志级别混淆

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4970513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 08:51:56  来源:igfitidea点击:

Java logging levels confusion

javaloggingjava.util.logging

提问by alex2k8

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

我将日志记录级别设置为 CONFIG,但看不到在 CONFIG 级别写入的消息。我缺少什么?

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

回答by wds

I typically use logbackto implement logging, which seems a tad better documented. So I would recommend switching to that.

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

But to answer your question, I think what is happening is that your Loggeris configured correctly, but the Handlerit's sending its messages to isn't. The default configuration probably attaches a handler with INFOlevel logging to the root logger.

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

edit: I wrote a little test program to verify, you indeed need to set the level on the handler attached to the root logger. You can do so like this:

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

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

Gives as output:

作为输出给出:

Feb 11, 2011 4:32:14 PM Test main
CONFIG: config

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

This sets the level for all handlers attached to this. Obviously a better choice would be writing your own options file and explicitly configuring your loggers. A quick google turned up this articleon the subject.

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

You could also try configuring with a properties file on your classpath that reads:

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

java.util.logging.ConsoleHandler.level=CONFIG

回答by Romain Linsolas

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)
  • 严重(最高值)
  • 警告
  • 信息
  • 配置
  • 美好的
  • 更精细
  • FINEST(最低值)

So in your case, if you define the level to INFO, you will only see SEVERE, WARNINGand INFOlogs, and not CONFIGmessages.

所以你的情况,如果您定义的级别INFO,你只能看到SEVEREWARNINGINFO日志,并没有CONFIG消息。



Edit, to answer your corrected question:

编辑,回答您更正的问题:

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

也许您的Logger类使用了第三方库(log4j、slf4j 等),并且该库定义了自己的日志级别。比如对于log4j,只有以下几个级别:

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL
  • 调试
  • 信息
  • 警告
  • 错误
  • 致命的

In such case, the level CONFIGis considered as a INFOlevel, that explains your current behavior.

在这种情况下,该级别CONFIG被视为一个INFO级别,可以解释您当前的行为。