1

I have an Eclipse Java project, let's call it 'unittest' and I am trying out log4j2 + SLF4J it seems to be logging ok, but ignoring my log4j2.xml. I am explicitly seeing: ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console.

Project file structure is where 'test' is the source folder for the project

unittest/
   test/
       log4j2.xml
       com/blah/blah/etc...

I put log4j2.xml in the root of the test/ folder. The output folder defined for the project is the unittets/bin directory. And I verify that the log4j2.xml is created in the bin directory when building my project. I am not using Maven, just Eclipse for now.

Here is my test code:

public class LogManagerConfigTest {

    private static final org.slf4j.Logger slf4jLogger = LoggerFactory.getLogger(LogManagerConfigTest.class);

    @Test
    public void loggerTest() {
        //for troubleshooting No log4j2 configuration file found error only
        System.out.println("Looking for configuration on classpath:");
        URL resource = ClassLoader.getSystemResource("log4j2.xml"); 
        System.out.println("found"+String.valueOf(resource));

        slf4jLogger.trace("Trace message Test {}",1);
        slf4jLogger.trace("Trace message Test {}",2);

        slf4jLogger.debug("Debug message Test {}",1);
        slf4jLogger.debug("Debug message Test {}",2);

        slf4jLogger.info("Info message Test {}",1);
        slf4jLogger.info("Info message Test {}",2);

        slf4jLogger.error("Error message Test {}",1);
        slf4jLogger.error("Error message Test {}",2);

    }

}

Contents of the log4j2.xml I took from the log4j documentation for now, I plan to change later...

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Logger name="sandbox" level="trace" additivity="false">
      <AppenderRef ref="Console"/>
    </Logger>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>

Running this as a JUnit test in Eclipse using the same classpath as my Eclipse project, the following is the output:

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Looking for configuration on classpath:
found file:/C:/work/workspace/unittests/bin/log4j2.xml
12:39:43.969 [main] ERROR sandbox.LogManagerConfigTest - Error message Test 1 
12:39:43.971 [main] ERROR sandbox.LogManagerConfigTest - Error message Test 2

So I am not sure what I am missing. Similar questions don't seem to have direct answer, that resolves my issue.

ammianus
  • 488
  • 7
  • 23

2 Answers2

1

Problem resolved itself and went away. I had tried to do Project > Clean and clean the unittest project to no apparent effect. Then tried out a workaround by creating a second xml file in the same folder of the project 'log4j2-test.xml', as soon as I added this everything worked. Now if I delete the log4j2-test.xml, it appears to be reading from log4j2.xml fine, but I haven't changed anything else. So a bit weird but no longer an issue.

ammianus
  • 488
  • 7
  • 23
-1

Maven: make sure that your pom.xml includes any xml files in your resources directory, as follows:

<resources>
    <resource>
    <directory>src/main/resources</directory>
        <includes>
            <include>**/*.xml</include>
             ... any other includes ....
        </includes>
    </resource>
</resources>`

Then clean (mvn clean) and build (mvn package) Eclipse will abide afterwards!

Ardent Coder
  • 3,777
  • 9
  • 27
  • 53