1. 程式人生 > >【已解決】maven環境下log4j配置檔案無效的解決方案

【已解決】maven環境下log4j配置檔案無效的解決方案

背景:學webmagic時跑官方案例,出現以下錯誤:

log4j:WARN No appenders could be found for logger (us.codecraft.webmagic.scheduler.QueueScheduler). log4j:WARN Please initialize the log4j system properly. log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

後來各種查資料讓在src加上log4j.properties配置檔案,然而並沒有什麼卵用。經過一番鑽研,終於知道了解決方案:

//1.自動快速地使用預設Log4j環境。   BasicConfigurator.configure (); 

//2.讀取使用Java的特性檔案編寫的配置檔案。 PropertyConfigurator.configure(String configFilename); 

//3.讀取XML形式的配置檔案,filename是配置檔案的路徑和檔名。 DOMConfigurator.configure(String filename);

必要時按要求引入需要的包,比如我在main函式裡使用的第2種,則需要

import org.apache.log4j.PropertyConfigurator;

最後run一下,心心念唸的日誌終於出來了!!!