Java日志记录级别混乱 [英] Java logging levels confusion

查看:247
本文介绍了Java日志记录级别混乱的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将日志记录级别设置为CONFIG,但是看不到在CONFIG级别上写的消息.我想念的是什么?

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

配置:

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

推荐答案

我通常使用 logback 来实现日志记录,似乎记录得更好.因此,我建议切换到该位置.

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

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

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:我编写了一个小测试程序来验证,您确实需要在根记录器附带的处理程序上设置级别.您可以这样做:

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

提供输出:

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

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

这将设置与此相关的所有处理程序的级别.显然,更好的选择是编写自己的选项文件并显式配置记录器.快速的Google出现了关于该主题的这篇文章.

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

这篇关于Java日志记录级别混乱的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆