状态的JavaDocsjava.util.logging.Level
:
按降序排列的级别是:
SEVERE
(最高值)WARNING
INFO
CONFIG
FINE
FINER
FINEST
(最低值)
资源
import java.util.logging.*;
class LoggingLevelsBlunder {
public static void main(String[] args) {
Logger logger = Logger.getAnonymousLogger();
logger.setLevel(Level.FINER);
System.out.println("Logging level is: " + logger.getLevel());
for (int ii=0; ii<3; ii++) {
logger.log(Level.FINE, ii + " " + (ii*ii));
logger.log(Level.INFO, ii + " " + (ii*ii));
}
}
}
输出
Logging level is: FINER
Jun 11, 2011 9:39:23 PM LoggingLevelsBlunder main
INFO: 0 0
Jun 11, 2011 9:39:24 PM LoggingLevelsBlunder main
INFO: 1 1
Jun 11, 2011 9:39:24 PM LoggingLevelsBlunder main
INFO: 2 4
Press any key to continue . . .
问题陈述
我的示例将 设置Level
为FINER
,因此我希望每个循环看到 2 条消息。相反,我看到每个循环都有一条消息(Level.FINE
消息丢失)。
问题
为了看到FINE
(, FINER
or FINEST
) 输出需要改变什么?
更新(解决方案)
感谢Vineet Reynolds 的回答,这个版本符合我的预期。它显示 3 xINFO
消息和 3 xFINE
消息。
import java.util.logging.*;
class LoggingLevelsBlunder {
public static void main(String[] args) {
Logger logger = Logger.getAnonymousLogger();
// LOG this level to the log
logger.setLevel(Level.FINER);
ConsoleHandler handler = new ConsoleHandler();
// PUBLISH this level
handler.setLevel(Level.FINER);
logger.addHandler(handler);
System.out.println("Logging level is: " + logger.getLevel());
for (int ii=0; ii<3; ii++) {
logger.log(Level.FINE, ii + " " + (ii*ii));
logger.log(Level.INFO, ii + " " + (ii*ii));
}
}
}