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

标签 java spring maven log4j2

目前我正在使用 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

但这需要我为每个环境构建一场新的 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 会更方便,因为可以将它们放在单个配置文件中,使用 --- 分隔文档。

关于java - 使用 Spring @Profile 加载 log4j2 xml 配置而不是 maven <profile>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40917737/

相关文章:

java - Maven 无法通过流操作链编译 java

java - 为什么在我编译我的应用程序时缺少一个类?

java - 分发这个框架的最佳方式

java - 从 Java 发送到 WebService 时,Zip 文件损坏

java - Spring : hibernate + ehcache

spring - Spring请求是否同步

java - 使 spring @Value 从静态字段中获取默认值

java - Maven : Missing artifact com. sun :tools:jar:1. 6.0 POM.xml 编译时异常

java - 删除数据库插入的冗余

java - 如何为 Github API 验证基本 Java 程序