Federico Xella
Federico Xella

Reputation: 137

log4j2 not finding configuration file

I've developed a Dynamic Web Project using Eclypse, and I would like to use log4j2 to log infos and errors in a file.

I made the configuration file but when I start the application this error appears:

ERROR StatusLogger No Log4j 2 configuration file found. Using default configuration (logging only errors to the console), or user programmatically provided configurations.

According to the documentation, log4j2 should look for log4j2.* file in the WEB-INF directory, but even if the file is in there, my application still can't see it.

The file is in .properties format, so no additional packages should be needed.

What am I doing wrong?

Upvotes: 2

Views: 1824

Answers (1)

sujith kasthoori
sujith kasthoori

Reputation: 184

Create a Logger Class say MyLogger, Log4j2 look for the config file location from property "log4j.configurationFile" , set that property in static block of the class. this will ensure the config file location is availble to Log4j2 during startup. You can place the config file external to the application.

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.io.File;
public class MyLogger { 
   static {   System.setProperty("log4j.configurationFile",System.getProperty("prop.loc")+File.separator+"MyLog4j2.xml");   
   }

   public static Logger getLogger(Class<?> clazz){
         return LogManager.getLogger(clazz);
   }
}

Calling class initilize logger as below

public class MyClass {

         private static final Logger logger = MyLogger.getLogger(MyImpl.class);

    public myMethod(){
        logger.info("My logs");
    }
}

Upvotes: 1

Related Questions