【问题标题】:How to use one log4j2 configuration file for runtime in IDE and another log4j2 configuration file for packaging/deployment?如何在 IDE 中使用一个 log4j2 配置文件进行运行时,使用另一个 log4j2 配置文件进行打包/部署?
【发布时间】:2022-01-20 06:32:44
【问题描述】:

使用 Eclipse 06-2021、Maven、log4j2。

我正在使用 log4j2 框架来登录我的应用程序。我在 log4j2.properties 文件的资源文件夹中创建了配置。

我在那里定义了一些记录器及其级别:

...
logger.filelogconvert.appenderRef = FileLogConvert
logger.fileloggeneral.appenderRef = FileLogGeneral
rootLogger.appenderRef.console.ref = FileLogGeneral

logger.fileloggeneral.level = warn
rootLogger.level = warn

但是,当我想部署应用程序时,我需要重写此文件,因为我需要将级别从 info 更改为 warn 或更改 appender/logger 引用。

【问题讨论】:

    标签: java maven configuration log4j2 properties-file


    【解决方案1】:

    使用 log4j2-test.properties 文件:https://logging.apache.org/log4j/2.x/manual/configuration.html#AutomaticConfiguration

    ...
    logger.filelogconvert.appenderRef = Console
    logger.fileloggeneral.appenderRef = Console
    rootLogger.appenderRef.console.ref = Console
    
    logger.fileloggeneral.level = info
    rootLogger.level = info
    

    然后在jar插件的pom.xml中排除:

    <build>
        ...
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <version>3.2.0</version>
            <configuration>
                <excludes>
                    <exclude>log4j2-test.properties</exclude>
                </excludes>
            </configuration>
        </plugin>
        ...
    </build>
    

    当然还有其他几种方法可以实现这一点。

    【讨论】:

      猜你喜欢
      • 2014-10-07
      • 1970-01-01
      • 2013-09-25
      • 2014-06-05
      • 2015-04-18
      • 2014-10-18
      • 2018-05-06
      • 1970-01-01
      • 2022-01-10
      相关资源
      最近更新 更多