Java 日志级别混乱

发布于 2024-10-16 18:03:52 字数 689 浏览 6 评论 0原文

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

配置:

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

测试:

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");

输出:

SEVERE: severe
WARNING: warning
INFO: info

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

捎一片雪花 2024-10-23 18:03:52

我通常使用 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

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

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

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

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 article on the subject.

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

java.util.logging.ConsoleHandler.level=CONFIG
掀纱窥君容 2024-10-23 18:03:52

这是预期的行为。

当您定义日志级别时,您将看到该级别的所有日志,以及链接到更高级别的日志。

顺序如下:

  • SEVERE(最高值)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST(最低值)

因此,在您的情况下,如果您将级别定义为 INFO,您只会看到 SEVERE< /code>、WARNINGINFO 日志,而不是 CONFIG 消息。


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

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

  • DEBUG
  • INFO
  • WARN
  • ERROR
  • FATAL

在这种情况下,级别 CONFIG 被视为 INFO 级别,它解释了您当前的级别行为。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文