状态的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
或FINEST
)输出,需要进行哪些更改?
更新(解决方案)
感谢Vineet Reynolds的回答,该版本可以按照我的期望工作。它显示3条INFO
消息和3条FINE
消息。
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));
}
}
}