Spring Logback 条件包括

问题描述 投票:0回答:1

嗨,所有 stackoverflow 专家,

我正在开发一个 springboot [2.5.1] 应用程序,我使用 logback.xml。我想将 logback.xml 的一些常用配置提取到 logback-include.xml 文件中。

基本上首先我想让

logback.xml
在 linux 服务器上包含
logback-include-file.xml
文件,如果
logback-include-file.xml
NOT 存在,那么我将从 jar 依赖项的资源文件夹中包含
logback-include.xml
。即,

1,

logback-include-file.xml
在 linux 服务器上。 //高优先级

2、

logback-include.xml
在一个jar依赖的resources文件夹中。 //低优先级

<if condition='property("logback.external.file").equalsIgnoreCase("true")'>
        <then>
            <include file="logback-include-file.xml"/> 
        </then>
        <else>
         <include resource="logback-include.xml"/>
</if>

大部分流程看起来不错,但我在检查

logback-include-file.xml
是否存在时遇到问题,即,

<if condition='property("logback.external.file").equalsIgnoreCase("true")'>

如果我在

logback.external.file=true
中放置一个硬编码的
application.properties

然后条件正常工作,但我想动态检查

logback-include-file.xml
是否存在并设置属性值。

<if condition='property("logback.external.file").isDefined()'> //even give errors which sound like no so much method

<dependency>
       <groupId>org.codehaus.janino</groupId>
       <artifactId>janino</artifactId>
       <version>3.1.9</version>
</dependency>

我在 springboot 应用程序

main
类中尝试了新的 Properties().setProperty()、ApplicationRunner、Environment 等,但都没有用。

如果有人能提出任何建议,我将不胜感激。

提前致谢。

java xml spring-boot properties logback
1个回答
0
投票
System.setProperty()

在 springboot 应用程序运行之前。

但最后,我没有采用这个,因为我更喜欢动态属性值刷新,仅仅在应用程序启动时检查是否存在外部文件没有多大意义

© www.soinside.com 2019 - 2024. All rights reserved.