首页 > 解决方案 > Spring Boot + Logback - 外部配置与本地配置

问题描述

我有一个带有本地 application.properties 文件的 Spring Boot 应用程序,其中包含:

logging.config=src/main/resources/local/logback-dev.xml

在这个 logback-dev.xml 文件中,有一个带有本地路径的文件附加程序(比如 /local/path/log/)

当部署到不同的环境(比如 PROD)时,部署的人使用他自己的 application.properties 文件作为外部化配置(--spring.config.location=...),其中包含:

logging.config=/prod/path/logback-prod.xml

在该 logback-prod.xml 文件中,有一个具有不同路径的文件附加程序(例如 /prod/path/log/)

运行应用程序时,出现错误,因为似乎两个文件都被利用了:我们在日志中出现 /prod/path/log/ 消息,例如“找不到路径 /local/path/log/

有人可以解释这里发生了什么吗?我认为外部化配置会覆盖本地配置,但这里有些奇怪。

标签: spring-bootlogbackproperties-file

解决方案


我们有同样的问题,并在本文档的帮助下找到了解决方案: 外部配置 https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-配置 应用程序属性 https://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-external-config-application-property-files

在 Pom 中,我们添加了:

<plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-jar-plugin</artifactId>
            <configuration>
                <excludes>
                    <exclude>**/application.properties</exclude>
                    <exclude>**/application-development.properties</exclude>
                    <exclude>**/application-production.properties</exclude>
                    <exclude>**/logback.xml</exclude>
                </excludes>
            </configuration>
        </plugin>

希望这能解决您的问题并帮助您理解。


推荐阅读