Creating Uber/Fat Jars with Log4j

1.2k views Asked by At

I am creating an uber jar using maven assembly plugin. One of the dependencies of my program is an application that can launch either by launching inside my program via a thread or can be launched independently as it is its own uberjar.

My problem is that both applications use log4j2 and both have a log4j2.xml in the resources folder. When maven makes my applications uberjar it overwrites my applications log4j.xml configuration file with the one from the dependency. Is there a way around this while still allowing both applications to have the config file in the resources folder?

1

There are 1 answers

1
TobiSH On BEST ANSWER

When Log4j starts it will locate the configuration with a weighted order from highest to lowest:

  1. Log4j will inspect the "log4j.configurationFile" system property and, if set, will attempt to load the configuration using the ConfigurationFactory that matches the file extension.
  2. If no system property is set the properties ConfigurationFactory will look for log4j2-test.properties in the classpath.
  3. If no such file is found the YAML ConfigurationFactory will look for log4j2-test.yaml or log4j2-test.yml in the classpath.
  4. If no such file is found the JSON ConfigurationFactory will look for log4j2-test.json or log4j2-test.jsn in the classpath.
  5. If no such file is found the XML ConfigurationFactory will look for log4j2-test.xml in the classpath.
  6. If a test file cannot be located the properties ConfigurationFactory will look for log4j2.properties on the classpath.
  7. If a properties file cannot be located the YAML ConfigurationFactory will look for log4j2.yaml or log4j2.yml on the classpath.
  8. If a YAML file cannot be located the JSON ConfigurationFactory will look for log4j2.json or log4j2.jsn on the classpath.
  9. If a JSON file cannot be located the XML ConfigurationFactory will try to locate log4j2.xml on the classpath.
  10. If no configuration file could be located the DefaultConfiguration will be used. This will cause logging output to go to the console.

So the easiest would be to move the your log4j.xml to a subfolder in your resources dir and define this one using the property "log4j.configurationFile". Otherwise just switch to a property base config file "log4j2.properties". Maybe this converter can help you.

Please also have a look at the documentation (that's where I got this list from).