java log4j:WARN 找不到记录器的附加程序 - 使用 slf4j-log4j12
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16726457/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
log4j:WARN No appenders could be found for logger - Using slf4j-log4j12
提问by Piero
It looks like this question has been asked many times. But all the solutions I've tried (mostly be sure that the log4j.properties file is in the correct location and it correctly typed) do not work in my case.
这个问题好像被问过很多次了。但是我尝试过的所有解决方案(主要是确保 log4j.properties 文件位于正确的位置并且正确键入)在我的情况下不起作用。
I have a maven project. I would like to use log4j for my tests. The test class uses a helper method defined in src/main/java where the logger is used.
我有一个 Maven 项目。我想使用 log4j 进行测试。测试类使用在使用记录器的 src/main/java 中定义的辅助方法。
In my helper class (in src/main/java/) I've imported
在我的助手类(在 src/main/java/ 中)我已经导入
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
and I have instantiated the logger
我已经实例化了记录器
private static final String TAG = Helper.class.getSimpleName();
private static final Logger logger = LoggerFactory.getLogger(TAG);
I've included both in src/main/resources and src/test/resources the following log4j.properties file
我在 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
In my POM I've included the dependency on slf4j
在我的 POM 中,我已经包含了对 slf4j 的依赖
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
</dependency>
In the code in my helper class I use the logger in this way
在我的助手类的代码中,我以这种方式使用记录器
logger.debug("logger test...");
No messages are printed in the console and I get the following warning message
控制台中没有打印任何消息,我收到以下警告消息
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.
What am I missing?
我错过了什么?
Update
更新
The issue was related to a project option that sets the log4j.configuration property to log4j-test.xml. I've added the following plugin to the project maven pom and this fixed the issue.
该问题与将 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>
回答by SubOptimal
From the parts you have posted it looks ok. So let's investigate the issue. I did a simple project (only 2 classes Helper: has a method to call your logger statement, AppTest: a JUnit test to call the method in Helper)
从您发布的部分来看,它看起来不错。所以让我们调查一下这个问题。我做了一个简单的项目(只有 2 个类 Helper:有一个方法来调用你的 logger 语句,AppTest:一个 JUnit 测试来调用 Helper 中的方法)
- clean up generated code
mvn clean
following files exist
./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 - compile the code
mvn compile
following files exist
./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
if the properties files is not copied into the target directory you need to find the reason - some starting points, check the output of
mvn -debug compile
mvn -Dlog4j.debug test
- 清理生成的代码
mvn clean
以下文件存在
./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 -debug compile
mvn -Dlog4j.debug test的输出
回答by Ganesh Gaxy
Try these Commands...
试试这些命令...
$ unset FLUME_HOME
$ 取消设置 FLUME_HOME
and Then Try.....
然后尝试.....
It worked for me....
它对我有用......