問題描述
log4j 沒有登錄到文件或控制台 (log4j is not logging into file or console)
我的 log4j2.properties 文件看起來像這樣
log4j.rootLogger=INFO, file, stdout
log4j.appender.file=org.apache.log4j.rolling.RollingFileAppender
log4j.appender.file.File=C:\\Users\\mchandak\\workspace\\Eclipse\\work\\LeadsDedupe\\logFile1.log
log4j.appender.file.MaxFileSize=1024MB
log4j.appender.file.MaxBackupIndex=1
log4j.appender.file.layout=org.apache.log4j.PatternLayout
log4j.appender.file.layout.ConversionPattern=%d{yyyy‑MM‑dd HH:mm:ss} %‑5p %c{1}:%L ‑ %m%n
下面是我的代碼:
import org.apache.logging.log4j.*;
public class Main {
private static Logger logger = LogManager.getLogger( Main.class.getName());
public static void main(String[] list){
logger.info("LOGGER IS WORKING");
}
}
代碼運行順利,沒有任何錯誤,但它沒有記錄任何內容。我的屬性文件對我來說看起來不錯。我用谷歌搜索了很多,但我無法理解我的代碼的問題。
參考解法
方法 1:
Your properties file is using the log4j‑1.x format. The new format is documented here: http://logging.apache.org/log4j/2.x/manual/configuration.html#Properties
You are not seeing any logging because the default configuration only logs at ERROR level.
(by Element、Remko Popma)