3

I have a problem concerning log4j2 which does not load the log4j2.xml configuration file in a project.

The project is bundled into an uber jar file. When running the application using java -jar jarfile.jar the application starts but log4j prints the following error to the console:

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

I checked the jar and it definitely contains a log4j2.xml file in the root location.

Because I could not figure out why this does not work I debugged to log4j2 bootstrap code. I found out that log4j never tries to read the log4j2.xml. This should happen in org.apache.logging.log4j.core.config.ConfigurationFactory.Factory#getConfiguration.

Unfortunately the list of factories used in this method is empty thus the method always returns null.

Any ideas on this?


If you want to check this clone https://github.com/cryptomator/cryptomator, cd to the main directory in the cloned repo and run mvn clean install -DskipTests -P uber-jar afterwards you will find the jar file in question under main/uber-jar/target.

Sebastian S
  • 4,420
  • 4
  • 34
  • 63
Markus Kreusch
  • 2,071
  • 3
  • 19
  • 33

2 Answers2

4

I suspect this is the same issue as Log4j2 configuration not found when running standalone application builded by shade plugin since it sounds like you are building an uber jar.

Community
  • 1
  • 1
rgoers
  • 8,696
  • 1
  • 22
  • 24
0

I have a similar issue, but not using shade. I have a jar file, with dependencies in /lib I have a log4j2.xml file in the same location as the main jar file.

I can (obviously) run the jar file by calling:

java -Dlog4j2.configurationFile=.\log4j2.xml -jar myjar.jar

However, in Windows, it's possible simply to double-click the jar file to load it. Everything loads and works, except that it doesn't find the log4j2.xml file - so no logfile is written.

What I would like to be able to do is have a simple jar file I can hand to someone and have it run on their machine, with the ability to configure logging in the event they run into issues.

EDIT: To do that, you need to amend your code thus:

public class MyClass
{
  static
  {
    System.setProperty("log4j.configurationFile", "log4j2.xml");
  }
  private final static Logger LOG = LogManager.getLogger();

  //OTHER STUFF HERE
}

Thanks to Load Log4j2 configuration file programmatically for the answer.

davejbur
  • 245
  • 1
  • 2
  • 7