log4j:WARN No appenders could be found for logger

在Java编程中有时候会遇到这样的错误:

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

其实就是IDE找不到log4j的properties文件,因为你还没把它加入到项目的class path。

以IntelliJ Idea为例,在src下新建一个文件夹,然后新建一个文件log4j.properties,往里面写入:

# Set root logger level to DEBUG and its only appender to A1.
log4j.rootLogger=DEBUG, A1

# A1 is set to be a ConsoleAppender.
log4j.appender.A1=org.apache.log4j.ConsoleAppender

# A1 uses PatternLayout.
log4j.appender.A1.layout=org.apache.log4j.PatternLayout
log4j.appender.A1.layout.ConversionPattern=%-4r [%t] %-5p %c %x - %m%n

保存,然后点击idea的File -> Project Structure -> Modules,选中刚才创建的resources文件夹,单机上面的Resources,这样就把这个目录下所有的资源文件加载到该项目的class path去啦。

log4j:WARN No appenders could be found for logger_第1张图片

再次运行项目,log正常打印出来了。

你可能感兴趣的:(Java)