为什么 Level.FINE 日志消息没有显示? [英] Why are the Level.FINE logging messages not showing?

查看:24
本文介绍了为什么 Level.FINE 日志消息没有显示?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

java.util.logging.Level 状态的 JavaDocs:

The JavaDocs for java.util.logging.Level state:

按降序排列的级别是:

  • SEVERE(最高值)
  • 警告
  • 信息
  • CONFIG
  • FINE
  • 更精细
  • FINEST(最低值)
  • SEVERE (highest value)
  • WARNING
  • INFO
  • CONFIG
  • FINE
  • FINER
  • FINEST (lowest value)
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 消息丢失).

Problem statement

My example sets the Level to FINER, so I was expecting to see 2 messages for each loop. Instead I see a single message for each loop (the Level.FINE messages are missing).

为了看到 FINE(、FINERFINEST)输出需要改变什么?

What needs changing in order to see the FINE (, FINER or FINEST) output?

感谢 Vineet Reynolds 的回答,这个版本符合我的预期.它显示 3 x INFO 消息,&3 x FINE 消息.

Thanks to Vineet Reynolds' answer, this version works according to my expectation. It displays 3 x INFO messages, & 3 x FINE messages.

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));
        }
    }
}

推荐答案

Loggers 只记录消息,即他们创建日志记录(或记录请求).它们不会将消息发布到目标,这是由处理程序处理的.设置记录器的级别,只会使其创建 匹配该级别或更高级别的日志记录.

Loggers only log the message, i.e. they create the log records (or logging requests). They do not publish the messages to the destinations, which is taken care of by the Handlers. Setting the level of a logger, only causes it to create log records matching that level or higher.

您可能正在使用 ConsoleHandler(我无法推断你的输出是System.err还是文件,但我假设是前者),默认发布Level级别的日志记录.INFO.您必须配置此处理程序,以发布 Level.FINER 及更高级别的日志记录,以获得所需的结果.

You might be using a ConsoleHandler (I couldn't infer where your output is System.err or a file, but I would assume that it is the former), which defaults to publishing log records of the level Level.INFO. You will have to configure this handler, to publish log records of level Level.FINER and higher, for the desired outcome.

我建议阅读 Java 日志记录概述 指南,以了解底层设计.该指南涵盖了 Logger 和 Handler 概念之间的区别.

I would recommend reading the Java Logging Overview guide, in order to understand the underlying design. The guide covers the difference between the concept of a Logger and a Handler.

编辑处理程序级别

1.使用配置文件

可以修改 java.util.logging 属性文件(默认情况下,这是 JRE_HOME/lib 中的 logging.properties 文件)可以更改默认级别控制台处理程序:

The java.util.logging properties file (by default, this is the logging.properties file in JRE_HOME/lib) can be modified to change the default level of the ConsoleHandler:

java.util.logging.ConsoleHandler.level = FINER

2.在运行时创建处理程序

不推荐这样做,因为它会导致覆盖全局配置.在整个代码库中使用它可能会导致记录器配置难以管理.

This is not recommended, for it would result in overriding the global configuration. Using this throughout your code base will result in a possibly unmanageable logger configuration.

Handler consoleHandler = new ConsoleHandler();
consoleHandler.setLevel(Level.FINER);
Logger.getAnonymousLogger().addHandler(consoleHandler);

这篇关于为什么 Level.FINE 日志消息没有显示?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆