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

Using Spring @Profile to load log4j2 xml configuration instead of 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>

但这需要我为每个环境构建一个新的 war,我想避免这种情况。有没有办法让我使用 Spring @Profile 注释来加载 log4j2 配置?

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

举个例子:-

application-dev.properties 文件中放置以下内容。可以使用 -Dspring.profiles.active=dev

激活此配置文件

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

application-prd.properties文件中放置以下内容。可以使用 -Dspring.profiles.active=prd

激活此配置文件

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

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

注意:如果使用 YAML,则更方便,因为这些可以放在单个配置文件中,使用 ---.

分隔文档