java logger 日志级别,Java日志记录级别混乱

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

解决方案

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值