Java 日志级别混乱
我将日志记录级别设置为 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通常使用 logback 来实现日志记录,这似乎有更好的记录。所以我建议改用它。
但为了回答你的问题,我认为发生的情况是你的 Logger 配置正确,但它发送消息的 Handler 没有正确配置。默认配置可能会将具有 INFO 级别日志记录的处理程序附加到根记录器。
编辑:我编写了一个小测试程序来验证,您确实需要在附加到根记录器的处理程序上设置级别。您可以这样做:
作为输出给出:
这设置附加到此的所有处理程序的级别。显然,更好的选择是编写自己的选项文件并显式配置记录器。快速谷歌搜索就找到了关于这个主题的这篇文章。
您还可以尝试使用类路径上的属性文件进行配置,其中包含:
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 theHandler
it's sending its messages to isn't. The default configuration probably attaches a handler withINFO
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:
Gives as output:
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:
这是预期的行为。
当您定义日志级别时,您将看到该级别的所有日志,以及链接到更高级别的日志。
顺序如下:
因此,在您的情况下,如果您将级别定义为
INFO
,您只会看到SEVERE< /code>、
WARNING
和INFO
日志,而不是CONFIG
消息。编辑,回答您更正的问题:
也许您的 Logger 类使用了第三方库(log4j、slf4j 等),并且该库定义了自己的级别的日志。例如,对于 log4j,只有以下级别:
在这种情况下,级别
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:
So in your case, if you define the level to
INFO
, you will only seeSEVERE
,WARNING
andINFO
logs, and notCONFIG
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:In such case, the level
CONFIG
is considered as aINFO
level, that explains your current behavior.