Log4j-2.6.2 Basic Configurator未配置日志记录级别

时间:2016-09-04 22:22:45

标签: java logging configuration log4j log4j2

我试图通过以下代码实现基本配置程序

package com.myapp.loggingutilities;

import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.BasicConfigurator;

public class LoggingUtilitiesApplication {
    static Logger currentLogger = Logger.getLogger(LoggingUtilitiesApplication.class);

    public static void main(String[] args) {
        BasicConfigurator.configure();
        currentLogger.debug("Application Started here");
        LoggerUtilityModel bar = new LoggerUtilityModel();
        bar.doIt();
        currentLogger.debug("Application ended");

    }
}

bar的类def是

package com.myapp.loggingutilities;
import org.apache.log4j.Logger;

public class LoggerUtilityModel {
    static Logger modelLogger = Logger.getLogger(LoggerUtilityModel.class);

    public void doIt() {
        modelLogger.debug("OPPS! DID IT AGAIN");
    }

}

我使用的是Log4j2,分发时已here。当我在构建路径中使用Log4j-to-slf4j和实现jar时,我总是得到SLF4J桥错误:

Exception in thread "main" java.lang.ExceptionInInitializerError
Caused by: java.lang.ClassCastException: org.apache.logging.slf4j.SLF4JLoggerContext cannot be cast to org.apache.logging.log4j.core.LoggerContext
    at org.apache.log4j.Logger$PrivateManager.getContext(Logger.java:59)

当我删除它们时,它没关系。但是没有打印日志消息

我认为BasicConfigurator应该为DEBUG配置根记录器,这样我的所有日​​志语句都应该通过(即任何等于或高于DEBUG级别的内容)。但是当我运行应用程序时,我在控制台上看不到任何日志消息。我错过了什么?

2 个答案:

答案 0 :(得分:1)

BasicConfigurator是一个log4j 1.2类,不能用于配置Log4j 2.基本上,org.apache.log4j包中的所有内容都是Log4j 1.2(旧),org.apache.logging.log4j命名空间中的所有内容都是对于Log4j 2.

如果您对Log4j 2的编程配置感兴趣,请参阅the manual page。我个人认为最简单的XML配置文件格式最容易使用。

您所看到的行为(仅限ERROR级别的控制台日志记录)是Log4j 2"默认配置",如果它找不到配置文件,它就会执行此操作尚未手动配置。

答案 1 :(得分:0)

问题似乎是log4j2.6.2的LEVEL设置。根据Apache的Log4J-2.6.2文档,所有默认日志消息都设置为DEBUG。

此外,您无法使用root logger更改日志记录级别。您需要为每个记录器更改它。一切都默认为ERROR。

相关问题