$ java -Dlog4j.configuration=file:///path/to/your/log4j2.xml -jar /path/to/your/jar_file.jar
Written to the console, you get
ERROR
I have been dealing with this problem for a while. I have changed everything as described in this post and even thought error occured. In that case make sure that you clean the project when changing settings in .xml or .properties file. In eclipse environment. Choose Project -> Clean
This sometimes can be thrown before the actual log4j2 configuration file found on the web servlet. at least for my case I think so. Cuz I already have in my web.xml
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>classpath:log4j2-app.xml</param-value>
</context-param>
and checking the log4j-web source; in class
org.apache.logging.log4j.web.Log4jWebInitializerImpl
there is the line;
String location = this.substitutor
.replace(this.servletContext.getInitParameter("log4jConfiguration"));
all those makes me think that this is temporary log before configuration found.
<?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>
<Root level="DEBUG">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
Create a new Text document and copy-paste the above code and save it as log4j2.xml.
Now copy this log4j2.xml file and paste it under your src folder of your Java project.
Run your java program again, you will see error is gone.
Stuffs I check to verify logging,
mvn dependency:tree | grep log ## ./gradlew dependencies | grep log
[INFO] +- com.prayagupd:log-service:jar:1.0:compile
[INFO] +- org.apache.logging.log4j:log4j-api:jar:2.6.2:compile
[INFO] +- org.apache.logging.log4j:log4j-core:jar:2.6.2:compile
[INFO] | \- commons-logging:commons-logging:jar:1.1.1:compile
val logConfig: PropertiesConfiguration = new PropertiesConfiguration("application.properties")
System.setProperty("log4j.configurationFile", logConfig.getString("log4j.config.file"))
println("log4j.configurationFile :: " + System.getProperty("log4j.configurationFile"))
or
Configurator.initialize(null, logConfig.getString("log4j.config.file"));
Also if auto detection is happening make sure the file name is log4j2.* not log4j.*
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.4.0</version>
</dependency>
This might throw an exception if can not find the json parser
[Fatal Error] log4j2.json:1:1: Content is not allowed in prolog.
i had same problem, but i noticed that i have no log4j2.xml in my project after reading on the net about this problem, so i copied the related code in a notepad and reverted the notepad file to xml and add to my project under the folder resources. it works for me.
<?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>
<Root level="DEBUG">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
If you don't have the fortune of the log4j-1.2.jar on your classpath as Renko points out in his comment, you will only see the message no log4j2 configuration file found.
This is a problem if there is an error in your configuration file, as you will not be told where the problem lies upon start-up.
For instance if you have a log4j2.yaml file which log4j2 fails to process, because for example, you have not configured a YAML parser for your project, or your config is simply incorrect. You will encounter the no log4j2 configuration file found message, with no further information. This is the case even if you have a valid log4j2.xml file, as log4j2 will only attempt to process the first configuration file it finds.
I've found the best way to debug the problem is to explicitly state the configuration file you wish to use as per the command line argument mentioned above.
-Dlog4j.configurationFile=
Hopefully this will help you pinpoint if the issue is actually caused by your classloader not finding the log4j2 configuration file or something else in your configuration.
Update
You can also use the below property to change the default level of the status logger to get further information:
-Dorg.apache.logging.log4j.simplelog.StatusLogger.level=<level>