How to use multiple configuration files for log4j2

一个人想着一个人 提交于 2019-12-22 08:14:11

问题


I am writing Java code that tests a Java library. The library includes its own log4j2 configuration as part of the distribution.

I would like to use log4j2 in my test code without modifying the library's configuration.

Is there a way to have a separate log4j2 configuration for my test code?

This is all running as command-line Java, no servers or web involvement at all.

EDIT to try to be more clear: What I want is to be able to configure loggers, appenders, etc for the test code to use , and at the same time have the library code use its own separate configuration file for its logging. The idea is to use log4j2 in my test code, but without having to change the library's configuration file. Since the library configuration file is part of the library's distribution, I don't want to change it for testing.


回答1:


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.




回答2:


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




回答3:


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




回答4:


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




回答5:


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



来源:https://stackoverflow.com/questions/23200891/how-to-use-multiple-configuration-files-for-log4j2

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