Using java.util.logging to log on the console

2019-03-17 11:33发布

I want simply to log on the console using java.util.Logging:

Logger log = Logger.getLogger("my.logger");
log.setLevel(Level.ALL);
ConsoleHandler handler = new ConsoleHandler();
handler.setFormatter(new SimpleFormatter());
log.addHandler(handler);
log.fine("hello world");

but this prints out nothing. What am I missing?

Thanks

3条回答
仙女界的扛把子
2楼-- · 2019-03-17 11:58

Very simple, a logger can have several handlers, with each a different level.

handler.setLevel(Level.ALL);
查看更多
【Aperson】
3楼-- · 2019-03-17 12:05

I'm no expert on java logging, but if you change log.fine() to log.info() it will print. There's something fishy about fine - in practice, I never used it. Hopefully somebody who knows more can answer that.

ADDED: Yes, fine is special. I found an earlier SO answer for this:

查看更多
Bombasti
4楼-- · 2019-03-17 12:09

Logging on the standard System.out stream could be easily done by adding a StreamHandler handler:

logger.addHandler(new StreamHandler(System.out, new SimpleFormatter()))
查看更多
登录 后发表回答