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

15,808

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
Share:
15,808
Piero
Author by

Piero

Updated on June 04, 2022

Comments

  • Piero
    Piero almost 2 years

    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>
    
  • Piero
    Piero almost 11 years
    Hi SubOptimal, The log4j.properties is correctly copied in the target/classes directory. However when I run mvn -Dlog4j.debug test command I get the following output: log4j: Trying to find [log4j-test.xml] using context classloader sun.misc.Launcher$AppClassLoader@b92d342. log4j: Trying to find [log4j-test.xml] using sun.misc.Launcher$AppClassLoader@b92d342 class loader. log4j: Trying to find [log4j-test.xml] using ClassLoader.getSystemResource(). log4j: Could not find resource: [log4j-test.xml]. It looks like it doesn't look for the .properties file. I can't understand why
  • SubOptimal
    SubOptimal almost 11 years
    Hi Piero, as it looks for log4j-test.xml, which is not a default log4j configuration file name, it must be configured somwhere. Probably there is an option -Dlog4j.configuration=log4j-test.xml in your configuration or the system property is set programmatically. Have a look for it.
  • Piero
    Piero almost 11 years
    Hi SubOptimal. Yes, I still don't know why and where this configuration variable is set. But I found a way to fix it. See my update. Thanks for the tip on how to show log4j debug output.