简体   繁体   中英

Creating Uber/Fat Jars with Log4j

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?

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM