我试图通过以下代码实现基本配置器
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
的类定义是
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 级别的内容)。但是当我运行应用程序时,我在控制台上看不到任何日志消息。我错过了什么吗?
最佳答案
BasicConfigurator
是一个 log4j 1.2 类,不能用于配置 Log4j 2。基本上,org.apache.log4j
包中的所有内容都是 Log4j 1.2(旧版)并且org.apache.logging.log4j
命名空间中的所有内容均适用于 Log4j 2。
如果您对 Log4j 2 的编程配置感兴趣,请参阅the manual page 。我个人认为新的简化 XML 配置文件格式最容易使用。
您看到的行为(仅在错误级别进行控制台日志记录)是 Log4j 2 的“默认配置”,如果找不到配置文件并且尚未手动配置,它就会执行此操作。
关于java - Log4j-2.6.2 基本配置器未配置日志记录级别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39322000/