我已经把log4j放到了我的buildpath中,但是当我运行我的应用程序时,我得到了以下消息:
log4j:WARN No appenders could be found for logger (dao.hsqlmanager).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
这些警告是什么意思?这里的阑尾是什么?
我在使用log4j2时也遇到了同样的问题。我的问题是由使用错误的依赖库引起的:
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<scope>runtime</scope>
</dependency>
相反,我应该使用:
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId>
<scope>runtime</scope>
</dependency>
在我的情况下,我有一个log4j2.xml定义在我的“资源”目录,并指定使用它:
System.setProperty("log4j.configurationFile", "log4j2.xml");
首先导入:
import org.apache.log4j.PropertyConfigurator;
然后将以下代码添加到main方法:
String log4jConfPath ="path to/log4j.properties";
PropertyConfigurator.configure(log4jConfPath);
创建一个路径为的文件,并将以下代码添加到该文件中。
log4j.rootLogger=INFO, stdout
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.Target=System.out
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{yy/MM/dd HH:mm:ss} %p %c{2}: %m%n