How to use multiple configuration files for log4j2

社会主义新天地 提交于 2019-12-05 12:08:00

This may be helpful:

  • Log4j2 will first look for log4j2-test.xml in the classpath
  • if that file is not found, it will look for log4j2.xml in the classpath

So one option is to copy the library's configuration (log4j2.xml) to log4j2-test.xml and add your own configuration to log4j2-test.xml.

Furthermore, Log4j2 supports XInclude in XML configuration, so you could use that feature to avoid duplicating the library's configuration in your log4j2-test.xml.

Log4j2 supports "Composite Configuration" which exactly matches your requirement. All you need to do is provide path to multiple files in log4j.configurationFile property. This can be passed from command line or added to log4j2.component.properties file in your application.

References: https://logging.apache.org/log4j/2.x/manual/configuration.html#CompositeConfiguration https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties

There are two step you can try to solve for your issue

  • Create your own configuration file with your custom name(eg: xyz.properties/.xml)
  • You must add the following line to your java runtime command

cmd> java -Dlog4j.configuration=location/xyz.properties

If you use diffent name for configuration rather log4j.properties/.xml file you need to configure that file at runtime by above command for more info have a look here..

Correct format for using an alternate XML file to log4j2.xml:

java -Dlog4j.configurationFile=./location/log4j2-custom.xml

Assuming ./location/log4j2-custom.xml exists and is the new XML to replace log4j2.xml in this run

See: https://github.com/kamalcph/Log4j2Examples/blob/master/src/main/java/in/co/nmsworks/log4j2/examples/CompositeConfigurationExample.java

Referencing https://logging.apache.org/log4j/2.x/manual/configuration.html states that you can add multiple comma separated files under log4j2.configurationFile property.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!