如何为log4j2使用多个配置文件 [英] How to use multiple configuration files for log4j2

查看:969
本文介绍了如何为log4j2使用多个配置文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写测试Java库的Java代码.该库包含自己的log4j2配置作为发行版的一部分.

I am writing Java code that tests a Java library. 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?

这都是作为命令行Java运行的,根本没有服务器或Web参与.

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

编辑以使内容更清楚: 我想要的是能够配置记录器,附加程序等供测试代码使用,并且同时 使库代码使用其自己的单独配置文件为它的日志记录.这个想法是在我的测试代码中使用log4j2,但不必更改库的配置文件.由于库配置文件是库分发的一部分,所以我不想更改它以进行测试.

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.

推荐答案

这可能会有所帮助:

  • Log4j2首先将在类路径中查找log4j2-test.xml
  • 如果找不到该文件,它将在类路径中查找log4j2.xml

因此,一种选择是将库的配置(log4j2.xml)复制到log4j2-test.xml,然后将自己的配置添加到log4j2-test.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配置中支持XInclude ,因此您可以使用它功能,以避免在您的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使用多个配置文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆