简体   繁体   中英

log4j:WARN No appenders could be found for logger - Using slf4j-log4j12

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.

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.

In my helper class (in src/main/java/) I've imported

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

### 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

<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.

  <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>

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)

  1. 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

  2. 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

  3. some starting points, check the output of

    mvn -debug compile
    mvn -Dlog4j.debug test

Try these Commands...

$ unset FLUME_HOME

and Then Try.....

It worked for me....

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM