log4j:WARN No appenders could be found for logger

问题场景

在项目中使用Log4j时,出现以下情况:

log4j:WARN No appenders could be found for logger (com.frank.Log4jDemo).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
log4j:WARN No appenders could be found for logger

问题原因

  1. 缺失log4j.properties或log4j.xml配置文件;
  2. log4j.properties配置文件为放在source文件夹下;
  3. log4j.properties配置文件编码不正确;

解决方案

排查问题步骤:

1.确认是否建立log4j.properties或log4j.xml配置文件;
2.确认配置文件是否名称为log4j.properties或log4j.xml;
3.确认配置文件编码是否为UTF-8
4.确认配置文件中的内容是否正确。

以IDEA为例
项目结构:
log4j:WARN No appenders could be found for logger
通过上图可以发现,项目中已经正确建立log4j.properties配置文件,且放置于resource下,文件编码为UTF-8,运行结果:
log4j:WARN No appenders could be found for logger

IDEA中目录设置:

在IDEA中,我们知道resource为资源目录,如果没有将其标记为Resource,那么log4j配置文件不会发不到classpath下,解决方案如下:
1.File–>Project Structure…
log4j:WARN No appenders could be found for logger
2.标记java为Source,resources为Resources
log4j:WARN No appenders could be found for logger
或者直接resource右键–>Mark Directory As–>Resources Root,如果已经标记过了,则不会显示该选项。
log4j:WARN No appenders could be found for logger

上一篇:No appenders could be found for logger [IntelliJ IDEA]


下一篇:log4j:WARN No appenders could be found for logger 报错