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

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

问题描述

JavaDocs java.util.logging.Level 州:

降序是:


  • 严重(最高价值)

  • 警告

  • INFO

  • CONFIG

  • FINE

  • FINER

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



输出



Output

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 . . .



问题陈述



我的例子设置了等级 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 (, FINER FINEST )输出?

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

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

Thanks to Vineet Reynolds' answer, this version works according to my expectation. It displays 3xINFO messages, & 3xFINE 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 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 Logging Overview 指南,以了解底层设计。该指南涵盖了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。使用配置文件

1. Using the Configuration file

java.util.logging属性文件(默认情况下,这是日志记录。属性 中的文件JRE_HOME / lib )可以修改以更改ConsoleHandler的默认级别:

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。在运行时创建处理程序

2. Creating handlers at runtime

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

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天全站免登陆