【问题标题】:How to use multiple configuration files for log4j2如何为 log4j2 使用多个配置文件
【发布时间】:2014-06-05 17:22:32
【问题描述】:

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

我想在我的测试代码中使用 log4j2 而不修改库的配置。

有没有办法为我的测试代码设置单独的 log4j2 配置?

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

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

【问题讨论】:

  • Log4j 从类路径加载配置文件。如果将测试代码类路径放在库类路径之前,则应从测试代码中获取 log4j 配置。这是 stackoverflow 问题的link,它可能会有所帮助。
  • 感谢 Alexey 的回复,但我认为我的问题不够清楚。我已经对其进行了编辑以使其更加清晰。

标签: java xml logging log4j2 xinclude


【解决方案1】:

这可能会有所帮助:

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

所以一种选择是将库的配置 (log4j2.xml) 复制到 log4j2-test.xml 并将您自己的配置添加到 log4j2-test.xml。

此外,Log4j2 supports XInclude in XML configuration,因此您可以使用该功能来避免在 log4j2-test.xml 中重复库的配置。

【讨论】:

  • 谢谢,事实上我最终合并了测试和分发 log4j2 文件。不是我想要的,但显然没有比这更好的了。我确实尝试过使用 Xinclude,但无法使其工作。花更多时间尝试使 Xinclude 策略发挥作用还不够重要。
【解决方案2】:

您可以尝试通过两个步骤来解决您的问题

  • 使用您的自定义名称创建您自己的配置文件(例如:xyz.properties/.xml)
  • 您必须将以下行添加到您的 java 运行时命令中

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

如果您使用不同的名称进行配置而不是 log4j.properties/.xml 文件,则需要在运行时通过上述命令配置该文件以获取更多信息,请查看 here..

【讨论】:

  • 你在哪里改变你的“java运行时命令”?我尝试了 Project > Preferences > Run/Debug Settings ,选择了一些配置,单击了 Edit,Arguments 选项卡,VM arguments。
【解决方案3】:

Log4j2 支持完全符合您要求的“复合配置”。您需要做的就是在log4j.configurationFile 属性中提供多个文件的路径。这可以从命令行传递或添加到应用程序的log4j2.component.properties 文件中。

参考资料: https://logging.apache.org/log4j/2.x/manual/configuration.html#CompositeConfiguration https://logging.apache.org/log4j/2.x/manual/configuration.html#SystemProperties

【讨论】:

    【解决方案4】:

    将备用 XML 文件用于 log4j2.xml 的正确格式:

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

    假设 ./location/log4j2-custom.xml 存在并且是本次运行中替换 log4j2.xml 的新 XML

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

    【讨论】:

      【解决方案5】:

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

      【讨论】:

        【解决方案6】:

        要使用多个配置文件,你必须根据环境设置。

        例如:

        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());
            }
        

        【讨论】:

          【解决方案7】:
          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
          

          【讨论】:

            猜你喜欢
            • 2014-10-07
            • 1970-01-01
            • 2014-10-18
            • 2022-01-20
            • 1970-01-01
            • 2020-10-15
            • 1970-01-01
            • 1970-01-01
            • 2013-08-10
            相关资源
            最近更新 更多