【已解决】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一下,心心念念的日志终于出来了!!!

参考文章:https://www.cnblogs.com/whoyou/p/5151702.html

猜你喜欢

转载自blog.csdn.net/muyao987/article/details/82967142