我試圖通過下面的代碼來實現基本的配置Log4j的-2.6.2基本配置沒有配置記錄等級
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。我總是得到SLF4J橋錯誤,當我有Log4j的到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級別)。但是當我運行該應用程序時,在控制檯上看不到任何日誌消息。我錯過了什麼?
我剛剛運行了你的代碼,並在控制檯中用'DEBUG'級別獲得了消息。 – DimaSan
@DimaSan我碰巧從我的構建路徑中刪除了slf4j綁定jar,因爲他們在抱怨衝突 - 你也這樣做了嗎? – ha9u63ar
我認爲問題在於缺少'slf4j'罐子,因爲我使用已經包含它的Spring Boot運行你的代碼。 – DimaSan