Java log4j:警告:找不到记录器的附加器-使用slf4j-log4j12
看起来这个问题已经被问过很多次了。但是我尝试过的所有解决方案(主要是确保log4j.properties文件位于正确的位置并正确键入)在我的情况下都不起作用 我有一个maven项目。我想使用log4j进行测试。测试类使用在使用记录器的src/main/java中定义的助手方法 在我的助手类(src/main/java/)中,我导入了Java log4j:警告:找不到记录器的附加器-使用slf4j-log4j12,java,maven,log4j,slf4j,Java,Maven,Log4j,Slf4j,看起来这个问题已经被问过很多次了。但是我尝试过的所有解决方案(主要是确保log4j.properties文件位于正确的位置并正确键入)在我的情况下都不起作用 我有一个maven项目。我想使用log4j进行测试。测试类使用在使用记录器的src/main/java中定义的助手方法 在我的助手类(src/main/java/)中,我导入了 import org.slf4j.Logger; import org.slf4j.LoggerFactory; 我已经实例化了记录器 private stati
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
我已经实例化了记录器
private static final String TAG = Helper.class.getSimpleName();
private static final Logger logger = LoggerFactory.getLogger(TAG);
我在src/main/resources和src/test/resources中都包含了以下log4j.properties文件
### set log levels - for more verbose logging change 'info' to 'debug' ###
### Also add logfile to the root, if need stdout then add stdout appender here###
log4j.rootLogger=debug, stdout
### direct log messages to stdout ###
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%d{dd-mm HH:mm:ss,SSS} %p/%c{1}:%L - %m%n
在我的POM中,我包含了对slf4j的依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
控制台中未打印任何消息,我收到以下警告消息
log4j:WARN No appenders could be found for logger (Helper).
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.configuration属性设置为log4j-test.xml的项目选项有关。我在项目maven pom中添加了以下插件,这解决了这个问题
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<systemPropertyVariables>
<log4j.configuration>log4j.properties</log4j.configuration>
</systemPropertyVariables>
</configuration>
</plugin>
org.apache.maven.plugins
maven surefire插件
2.14.1
log4j.properties
从您发布的部分来看,它看起来不错。那么让我们来调查一下这个问题。
我做了一个简单的项目(只有两个类Helper:有一个方法调用您的logger语句,AppTest:一个JUnit测试调用Helper中的方法)
mvn清洁
存在以下文件
./src/main/java/sub/optimal/mavenexample/Helper.java
/src/main/resources/log4j.properties
./src/test/java/sub/optimal/mavenexample/AppTest.java
/src/test/resources/log4j.properties
mvn编译
存在以下文件
./src/main/java/sub/optimal/mavenexample/Helper.java
./src/main/resources/log4j.properties
./src/test/java/sub/optimal/mavenexample/AppTest.java
./src/test/resources/log4j.properties
/target/classes/log4j.properties
./target/classes/sub/optimal/mavenexample/Helper.class
如果未将属性文件复制到目标目录,则需要查找原因
的输出 mvn-调试编译
mvn-Dlog4j.debug测试
它对我起作用了……嗨,次优,log4j.properties被正确地复制到target/classes目录中。但是,当我运行mvn-Dlog4j.debug test命令时,我得到以下输出:log4j:尝试使用上下文类加载器sun.misc.Launcher查找[log4j test.xml]$AppClassLoader@b92d342. log4j:尝试使用sun.misc.Launcher查找[log4j test.xml]$AppClassLoader@b92d342类加载器。log4j:尝试使用ClassLoader.getSystemResource()查找[log4j test.xml]。log4j:找不到资源:[log4j test.xml]。看起来它没有查找.properties文件。我无法理解为什么Piero会这样做,因为它会查找log4j-test.xml,这不是默认的log4j配置文件名,它必须在哪里配置。您的配置中可能有一个选项
-Dlog4j.configuration=log4j test.xml
,或者系统属性是以编程方式设置的。看看吧。嗨,次优。是的,我仍然不知道这个配置变量设置的原因和位置。但我找到了一个解决办法。查看我的更新。感谢您提供有关如何显示log4j调试输出的提示。将-Dlog4j.debug
添加到JVM参数中,以查看log4j拾取的是哪个配置文件
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.14.1</version>
<configuration>
<systemPropertyVariables>
<log4j.configuration>log4j.properties</log4j.configuration>
</systemPropertyVariables>
</configuration>
</plugin>