简体   繁体   English

log4j2如何使用多个配置文件

[英]How to use multiple configuration files for log4j2

I am writing Java code that tests a Java library.我正在编写测试 Java 库的 Java 代码。 The library includes its own log4j2 configuration as part of the distribution.该库包含自己的 log4j2 配置作为分发的一部分。

I would like to use log4j2 in my test code without modifying the library's configuration.我想在我的测试代码中使用 log4j2 而不修改库的配置。

Is there a way to have a separate log4j2 configuration for my test code?有没有办法为我的测试代码设置单独的 log4j2 配置?

This is all running as command-line Java, no servers or web involvement at all.这一切都作为命令行 Java 运行,根本没有服务器或 Web 参与。

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.这个想法是在我的测试代码中使用 log4j2,但不必更改库的配置文件。 Since the library configuration file is part of the library's distribution, I don't want to change it for testing.由于库配置文件是库发行版的一部分,我不想为了测试而更改它。

This may be helpful:这可能会有所帮助:

  • Log4j2 will first look for log4j2-test.xml in the classpath Log4j2 将首先在类路径中查找 log4j2-test.xml
  • if that file is not found, it will look for log4j2.xml in the classpath如果未找到该文件,它将在类路径中查找 log4j2.xml

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.因此,一种选择是将库的配置(log4j2.xml)复制到 log4j2-test.xml 并将您自己的配置添加到 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支持 XML 配置中的 XInclude ,因此您可以使用该功能来避免在 log4j2-test.xml 中复制库的配置。

Log4j2 supports "Composite Configuration" which exactly matches your requirement. Log4j2 支持完全符合您要求的“复合配置”。 All you need to do is provide path to multiple files in log4j.configurationFile property.您需要做的就是在log4j.configurationFile属性中提供多个文件的路径。 This can be passed from command line or added to log4j2.component.properties file in your application.这可以从命令行传递或添加到应用程序中的log4j2.component.properties文件中。

References: https://logging.apache.org/log4j/2.x/manual/configuration.html#CompositeConfiguration https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties参考资料: 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)使用您的自定义名称创建您自己的配置文件(例如:xyz.properties/.xml)
  • You must add the following line to your java runtime command您必须将以下行添加到您的 java 运行时命令

cmd> java -Dlog4j.configuration=location/xyz.properties 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..如果您使用不同的名称进行配置而不是 log4j.properties/.xml 文件,您需要在运行时通过上述命令配置该文件以获取更多信息,请查看此处..

Correct format for using an alternate XML file to log4j2.xml:将备用 XML 文件用于 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假设./location/log4j2-custom.xml存在并且是在此运行中替换 log4j2.xml 的新 XML

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

参考https://logging.apache.org/log4j/2.x/manual/configuration.html指出您可以在 log4j2.configurationFile 属性下添加多个逗号分隔的文件。

To use multiple configuration files, depending on the environment you must set the.要使用多个配置文件,您必须根据环境进行设置。

for example:例如:

if (env.equals("DEV")) {
 setConfigFile("log4j2-dev.xml");
}

public static void setConfigFile(String logConfigFile) {
        File file = new File(logConfigFile);
        LoggerContext context = (org.apache.logging.log4j.core.LoggerContext) LogManager.getContext(false);
        context.setConfigLocation(file.toURI());
    }
first configure application.yaml file
spring:
  profiles:
    active: dev

---
spring:
  message: running in the dev profile    //just to output the message in the log
  profiles: dev
logging.config: classpath:log4j2-dev.xml

---
spring:
  profiles: prod
logging.config: classpath:log4j2-prod.xml



and create these similar files in your classpath
* log4j2-dev.xml
* log4j2-prod.xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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