Pie*_*ero 5 java log4j slf4j maven
看起来这个问题已被多次询问过.但是我尝试过的所有解决方案(主要是确保log4j.properties文件位于正确的位置并且输入正确)在我的情况下不起作用.
我有一个maven项目.我想使用log4j进行测试.测试类使用在src/main/java中定义的辅助方法,其中使用了记录器.
在我的助手类(在src/main/java /中)我已导入
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Run Code Online (Sandbox Code Playgroud)
我已经实例化了记录器
private static final String TAG = Helper.class.getSimpleName();
private static final Logger logger = LoggerFactory.getLogger(TAG);
Run Code Online (Sandbox Code Playgroud)
我在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
Run Code Online (Sandbox Code Playgroud)
在我的POM中,我已经包含了对slf4j的依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
Run Code Online (Sandbox Code Playgroud)
在我的助手类的代码中,我以这种方式使用记录器
logger.debug("logger test...");
Run Code Online (Sandbox Code Playgroud)
控制台中没有打印消息,我收到以下警告消息
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.
Run Code Online (Sandbox Code Playgroud)
我错过了什么?
更新
该问题与将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>
Run Code Online (Sandbox Code Playgroud)
从您发布的部分看起来没问题.让我们来研究一下这个问题吧.我做了一个简单的项目(只有2个类Helper:有一个方法来调用你的logger语句,AppTest:一个JUnit测试来调用Helper中的方法)
| 归档时间: |
|
| 查看次数: |
13929 次 |
| 最近记录: |