Log4j2 configuration - No log4j2 configuration file found
Asked Answered
P

7

98

Lately I decided to learn how to use the log4j2 logger. I downloaded required jar files, created library, xml comfiguration file and tried to use it. Unfortunately i get this statement in console (Eclipse) :

ERROR StatusLogger No log4j2 configuration file found. Using default configuration:       logging only errors to the console.

This is my testing class code:

package log4j.test;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

public class Log4jTest
{
    static final Logger logger = LogManager.getLogger(Logger.class.getName());

    public static void main(String[] args) {    
        logger.trace("trace");
        logger.debug("debug");
        logger.info("info");
        logger.warn("warn");
        logger.error("error");
        logger.fatal("fatal");
    }
}

And my xml config file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration package="log4j.test" 
               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="log4j.test.Log4jTest" level="trace">
            <AppenderRef ref="Console"/>
        </Logger>
        <Root level="trace">
            <AppenderRef ref="Console"/>
        </Root>
    </Loggers>
</Configuration>

I tried also with xml without <Logger> tags, and package specification and in various folders/packages directories, but it didn't help. Now my log4j2.xml file is located directly in project folder in eclipse.

Postmistress answered 25/8, 2014 at 13:33 Comment(2)
The doc sais, it just has to be on the classpath. Is it?Wyne
yeah, I missed it. Sorry for stupid question and thanks :)Postmistress
P
148

Is this a simple eclipse java project without maven etc? In that case you will need to put the log4j2.xml file under src folder in order to be able to find it on the classpath. If you use maven put it under src/main/resources or src/test/resources

Physiognomy answered 25/8, 2014 at 13:51 Comment(6)
My hero! I had the configuration file in the classpath (I think), but it was not in the src folder. I moved it in there and it is working now!Anthesis
how is configurate with a maven project? (like the maven way because i suppose if i put a 'log4j2.xml' under the classpath i would work), thanks for the tips :)Chausses
log4j2.xml is in my src (JavaFX Project without maven) but it is not found.. is this soooo complicated?!?! :(Forebode
For sbt the log4j2.xml goes into src/main/resources as well.Remarque
i cant find this file log4j2.xmlSever
In my case I had to exclude the spring-boot-starter-logging module before this started working.Enrol
C
35

You need to choose one of the following solutions:

  1. Put the log4j2.xml file in resource directory in your project so the log4j will locate files under class path.
  2. Use system property -Dlog4j.configurationFile=file:/path/to/file/log4j2.xml
Crandell answered 13/9, 2015 at 11:41 Comment(1)
-Dlog4j.configurationFile is what I needed, since the project I am working on is not Maven based. AwesomeAlbertoalberts
T
18

Was following the documentations - Apache Log4j2 Configuratoin and Apache Log4j2 Maven in configuring log4j2 with yaml. As per the documentation, the following maven dependencies are required:

  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.8.1</version>
  </dependency>
  <dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.8.1</version>
  </dependency>

and

<dependency>
    <groupId>com.fasterxml.jackson.dataformat</groupId>
    <artifactId>jackson-dataformat-yaml</artifactId>
    <version>2.8.6</version>
</dependency>

Just adding these didn't pick the configuration and always gave error. The way of debugging configuration by adding -Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE helped in seeing the logs. Later had to download the source using Maven and debugging helped in understanding the depended classes of log4j2. They are listed in org.apache.logging.log4j.core.config.yaml.YamlConfigurationFactory:

com.fasterxml.jackson.databind.ObjectMapper
com.fasterxml.jackson.databind.JsonNode
com.fasterxml.jackson.core.JsonParser
com.fasterxml.jackson.dataformat.yaml.YAMLFactory

Adding dependency mapping for jackson-dataformat-yaml will not have the first two classes. Hence, add the jackson-databind dependency to get yaml configuration working:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.8.6</version>
</dependency>

You may add the version by referring to the Test Dependencies section of log4j-api version item from MVN Repository. E.g. for 2.8.1 version of log4j-api, refer this link and locate the jackson-databind version.

Moreover, you can use the below Java code to check if the classes are available in the classpath:

System.out.println(ClassLoader.getSystemResource("log4j2.yml")); //Check if file is available in CP
ClassLoader cl = Thread.currentThread().getContextClassLoader(); //Code as in log4j2 API. Version: 2.8.1
 String [] classes = {"com.fasterxml.jackson.databind.ObjectMapper",
 "com.fasterxml.jackson.databind.JsonNode",
 "com.fasterxml.jackson.core.JsonParser",
 "com.fasterxml.jackson.dataformat.yaml.YAMLFactory"};

 for(String className : classes) {
     cl.loadClass(className);
 }
Thorium answered 6/4, 2017 at 16:8 Comment(1)
Thank you. jackson-databind did solve my problemOozy
H
4

Eclipse will never see a file until you force a refresh of the IDE. Its a feature! So you can put the file all over the project and Eclipse will ignore it completely and throw these errors. Hit refresh in Eclipse project view and then it works.

Hjerpe answered 23/2, 2016 at 19:2 Comment(0)
M
4

Additionally to what Tomasz W wrote, by starting your application you could use settings:

-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=TRACE 

to get most of configuration problems.

For details see Log4j2 FAQ: How do I debug my configuration?

Mauretta answered 5/5, 2017 at 7:57 Comment(0)
C
0

In my case I had to put it in the bin folder of my project even the fact that my classpath is set to the src folder. I have no idea why, but it's worth a try.

Canonize answered 7/6, 2017 at 16:17 Comment(1)
This suggests that the project does not recognise that the folder containing your config is a resource folder so it doesn't copy the file over automatically as part of the build. I'd suggest looking into this further because eventually you will make a change to the source that won't get copied to the bin folder and end up spending ages wondering why your change didn't work.Duo
S
0

log4j-test.xml => name of the configuration file if placed in /test/resources

     @TestPropertySource(properties = {
            "log4j.configurationFile","log4j-test.xml"
       })
     @CucumberContextConfiguration
     @ContextConfiguration(classes = TestConfiguration.class)
     public class Steps{
      private Logger log = LogManager.getLogger(Steps.class);

      //implementation
     }
Seedbed answered 28/2, 2022 at 13:54 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.