【问题标题】:Debug logging is not working on specific handler调试日志记录不适用于特定处理程序
【发布时间】:2020-05-22 04:32:34
【问题描述】:

我正在使用 JDK14Logger 实现 apache commons-loggings 框架。调试日志没有出现,它们仅在我将根记录器设置为 FINE 时出现。我的理解是,设置为特定处理程序的日志级别应该覆盖根记录器的日志级别。然而,这并没有发生。

# The following creates the console handler
handlers=java.util.logging.ConsoleHandler, java.util.logging.FileHandler

# Set the default logging level for the root logger
.level=FINE

# Set the default logging level
java.util.logging.ConsoleHandler.level=FINE
java.util.logging.FileHandler.level=FINEST

# log level for the "com.rst.example" package


# Set the default formatter
java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter
java.util.logging.FileHandler.formatter=java.util.logging.SimpleFormatter

# Specify the location and name of the log file
java.util.logging.FileHandler.pattern=D:/test.log

测试类:

public class Test {
private  Log logger = LogFactory.getLog(Test.class.getName());

 static  {
    System.getProperties().setProperty("java.util.logging.config.file","log-config.properties");
}

public static void main(String[] args) {
    //-Djava.util.logging.config.file=src/main/resources/log-config.properties

    Test test =  new Test();

    test.logger.info("info from main");
    test.logger.error("error from main");
    test.logger.fatal("fatal from main");
    System.out.println("is dubug enabled? :" + test.logger.isDebugEnabled());
    test.logger.debug("debug from main");

}

}

【问题讨论】:

    标签: java logging apache-commons


    【解决方案1】:

    默认情况下,所有 JDK 记录器都会将日志记录发布到根记录器的处理程序。 ConsoleHandler 的默认级别是INFO

    我的理解是,设置为特定处理程序的日志级别应该覆盖根记录器的日志级别。然而,这并没有发生。

    它没有,也不是。 Use DebugLogging to test your configuration file against the JDK logging.

    调试日志没有出现,只有当我将根记录器设置为 FINE 时才会出现。

    输出被发布到处理程序。未出现意味着您的处理程序未附加,级别未设置为您期望的级别,或者处理程序附加到不在发布路径上的子记录器。

    修改链接答案中的 DebugLogging 类以包含您的配置和执行日志记录的 apache-commons 代码。不要删除 JDK 日志记录代码。该代码的输出将引导您解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-19
      • 1970-01-01
      • 1970-01-01
      • 2022-01-10
      • 1970-01-01
      • 2023-01-10
      • 1970-01-01
      相关资源
      最近更新 更多