有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java使用Spring@Profile加载log4j2 xml配置,而不是maven<Profile>

目前,我正在使用maven加载log4j2配置,如下所示

<profiles>
    <profile>
        <id>dev</id>
        <activation>
            <activeByDefault>true</activeByDefault>
        </activation>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources/env-dev</directory>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>test</id>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources/env-test</directory>
                </resource>
            </resources>
        </build>
    </profile>
    <profile>
        <id>prd</id>
        <build>
            <resources>
                <resource>
                    <directory>src/main/resources/env-prd</directory>
                </resource>
            </resources>
        </build>
    </profile>
</profiles>

enter image description here

但这需要我为每个环境构建一场新的战争,我希望避免这场战争。我有没有办法使用Spring@Profile注释来加载log4j2配置


共 (1) 个答案

  1. # 1 楼答案

    我没有尝试使用log4j2,但我相信您可以使用属性logging.config提供配置文件位置

    例如:

    application-dev.properties文件中放置以下内容。可以使用-Dspring激活此配置文件。简介。活动=开发

    logging.config=/env-test/log4j2.xml

    application-prd.properties文件中放置以下内容。可以使用-Dspring激活此配置文件。简介。活动=珠三角

    logging.config=/env-prd/log4j2.xml

    将两个配置文件都放在src/main/resources目录中

    注意:如果使用YAML更方便,因为它们可以放在单个配置文件中,使用 -分隔文档