为什么java.util.logging级别:FINE,FINER和FINEST不起作用?

时间:2016-02-24 20:24:45

标签: java logging java.util.logging

我们目前正致力于使用java.util.logging添加日志记录。与许多其他线程建议的一样,我们已使用属性文件将loggerhandler级别调整为:ALL。输出将显示这些日志级别的日志消息:

  • CONFIG
  • INFO
  • WARNING
  • SEVERE

但不是在以下日志级别记录消息:

  • FINE
  • FINER
  • FINEST

这是我们的配置文件:

#Level for the logger. 
.level= ALL

#Controls the console handler
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level = ALL

#Adjust this level to change which logging is output
ca.fcl.wam.util.Logging.level = ALL

添加的日志记录语句如下:

log.log(Level.FINEST, "Finest");
log.log(Level.FINER, "FINER");
log.log(Level.FINE, "FINE");
log.log(Level.CONFIG, "CONFIG");
log.log(Level.INFO, "INFO");
log.log(Level.WARNING, "WARNING");
log.log(Level.SEVERE, "SEVERE");
log.finest("Finest Log");
log.finer("Finer Log");
log.fine("Fine Log");
log.config("Config Log");
log.info("Info Log");
log.warning("Warning Log");
log.severe("Severe Log");

我们的日志最终为:

[2/24/16 14:16:41:691 CST] 0000005f Logging       C   CONFIG
[2/24/16 14:16:41:691 CST] 0000005f Logging       I   INFO
[2/24/16 14:16:41:691 CST] 0000005f Logging       W   WARNING
[2/24/16 14:16:41:691 CST] 0000005f Logging       E   SEVERE
[2/24/16 14:16:41:691 CST] 0000005f Logging       C   Config Log
[2/24/16 14:16:41:691 CST] 0000005f Logging       I   Info Log
[2/24/16 14:16:41:691 CST] 0000005f Logging       W   Warning Log
[2/24/16 14:16:41:691 CST] 0000005f Logging       E   Severe Log

更改配置文件中的级别行为正常,除非它设置为:ALLFINEFINERFINEST。仅在每种情况下。

2 个答案:

答案 0 :(得分:0)

包含代码,用于输出在执行日志测试后所有记录器已设置的内容。

public class DebugLogging {

    private static final Logger log = Logger.getLogger("ca.fcl.wam.util.Logging");

    public static void main(String[] a) {
        log.log(Level.FINEST, "Finest");
        log.log(Level.FINER, "FINER");
        log.log(Level.FINE, "FINE");
        log.log(Level.CONFIG, "CONFIG");
        log.log(Level.INFO, "INFO");
        log.log(Level.WARNING, "WARNING");
        log.log(Level.SEVERE, "SEVERE");
        log.finest("Finest Log");
        log.finer("Finer Log");
        log.fine("Fine Log");
        log.config("Config Log");
        log.info("Info Log");
        log.warning("Warning Log");
        log.severe("Severe Log");
        printConfig();
    }

    private static void printConfig() {
        LogManager lm = LogManager.getLogManager();
        synchronized (lm) {
            Enumeration<String> e = lm.getLoggerNames();
            while (e.hasMoreElements()) {
                Logger l = lm.getLogger(e.nextElement());
                if (l != null) {
                    print(l, System.err);
                }
            }
        }
    }

    private static void print(Logger l, PrintStream ps) {
        String scn = l.getClass().getSimpleName();
        ps.append("scn=").append(scn).append(", n=").append(l.getName())
                .append(", l=").append(String.valueOf(l.getLevel()))
                .append(", fl=").println(l.getFilter());
        for (Handler h : l.getHandlers()) {
            ps.append("\t").append(scn).append("->")
                    .append(h.getClass().getName()).append(", h=")
                    .append(String.valueOf(h.getLevel())).append(", fl=")
                    .append(String.valueOf(h.getFilter()));
        }
    }
}

答案 1 :(得分:0)

如果这确实是您的整个配置文件,那么您将缺少设置处理程序的部分:

handlers = java.util.logging.ConsoleHandler
.level = FINE
java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter
java.util.logging.ConsoleHandler.level = FINE

适合我。