Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log4j2 configuration - No log4j2 configuration file found

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.

like image 831
Qbisiek Avatar asked Aug 25 '14 13:08

Qbisiek


People also ask

How do I resolve ERROR Statuslogger no log4j2 configuration file found?

Well, a solution is simplexml version="1.0" encoding="UTF-8"?> Try running your program again and you shouldn't see this issue anymore. Hope this helps.

What is log4j2 configuration file?

Configuration of Log4j 2 can be accomplished in 1 of 4 ways: Through a configuration file written in XML, JSON, YAML, or properties format. Programmatically, by creating a ConfigurationFactory and Configuration implementation.

Where is log4j configuration file?

The file is named log4j. properties and is located in the $DGRAPH_HOME/dgraph-hdfs-agent/lib directory. The file defines the ROLLINGFILE appenders for the root logger and also sets the log level for the file.

Where do I put my log4j2 xml file?

We should put log4j2. xml anywhere in the application's classpath. Log4j will scan all classpath locations to find out this file and then load it. We can find this file mostly placed in the 'src/main/resources' folder.


2 Answers

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

like image 97
Tomasz W Avatar answered Oct 25 '22 17:10

Tomasz W


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
like image 29
Abdulghaffar Al-Labadi Avatar answered Oct 25 '22 18:10

Abdulghaffar Al-Labadi