在 spring-boot 中为 logback 属性 指定默认值
Specify default value for a logback property, in spring-boot
在 spring-boot
应用程序中,我正在尝试为 logback
配置默认目录。
通常,在logback.xml
我会这样配置:
<property name="logFile.dir" value="${catalina.home:-/tmp}/logs" />
分隔符是:-
.
但是,在 application.properties
:
我必须这样配置:
logging.file=${catalina.home:/tmp}/logs/sportslight.log
需要将分隔符从 :-
更改为 :
。
问题是:
- 在
logback.xml
中,哪个是正确的分隔符,:-
或 :
?
- 在
application.properties
中,为什么只有:
有效,是因为spring-boot会先处理它,然后再将值传递给logback吗?
在 logback.xml 中,正确的分隔符是 :-
。 the logback docs 中的更多详细信息。
在 Spring 中,正确的分隔符是 :
,因为 Spring 支持 ${my.property:defaultValue}
语法。 the PlaceholderConfigurerSupport doc.
中有更多详细信息
因此,当面临变量替换的默认值分隔符选择时,logback 作者选择 :-
而 Spring 作者选择 :
。
在logback.xml或logback-spring.xml中,您可以为系统属性和项目设置默认值属性(或者你可以说 spring 属性)。
1) 对于系统 属性,您可以简单地使用 :-
语法。在下面的示例中,ThresholdFilter
的默认级别是 ERROR
.
<configuration>
<appender name="sentry" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${sentryLevel:-ERROR}</level>
</filter>
</appender>
</configuration>
您可以通过启动 java 进程来覆盖它,例如 -DsentryLevel=INFO
。
2) 对于项目 property/spring 属性,您可以在 springProperty
元素中设置 defaultValue
。
<configuration>
<springProperty scope="context" name="sentryLevel" source="your.prop.path" defaultValue="ERROR"/>
<appender name="sentry" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${sentryLevel}</level>
</filter>
</appender>
</configuration>
您可以通过更改 application.properties 或 application.yml 来覆盖它。
your.prop.path=INFO
在 spring-boot
应用程序中,我正在尝试为 logback
配置默认目录。
通常,在logback.xml
我会这样配置:
<property name="logFile.dir" value="${catalina.home:-/tmp}/logs" />
分隔符是:-
.
但是,在 application.properties
:
我必须这样配置:
logging.file=${catalina.home:/tmp}/logs/sportslight.log
需要将分隔符从 :-
更改为 :
。
问题是:
- 在
logback.xml
中,哪个是正确的分隔符,:-
或:
? - 在
application.properties
中,为什么只有:
有效,是因为spring-boot会先处理它,然后再将值传递给logback吗?
在 logback.xml 中,正确的分隔符是 :-
。 the logback docs 中的更多详细信息。
在 Spring 中,正确的分隔符是 :
,因为 Spring 支持 ${my.property:defaultValue}
语法。 the PlaceholderConfigurerSupport doc.
因此,当面临变量替换的默认值分隔符选择时,logback 作者选择 :-
而 Spring 作者选择 :
。
在logback.xml或logback-spring.xml中,您可以为系统属性和项目设置默认值属性(或者你可以说 spring 属性)。
1) 对于系统 属性,您可以简单地使用 :-
语法。在下面的示例中,ThresholdFilter
的默认级别是 ERROR
.
<configuration>
<appender name="sentry" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${sentryLevel:-ERROR}</level>
</filter>
</appender>
</configuration>
您可以通过启动 java 进程来覆盖它,例如 -DsentryLevel=INFO
。
2) 对于项目 property/spring 属性,您可以在 springProperty
元素中设置 defaultValue
。
<configuration>
<springProperty scope="context" name="sentryLevel" source="your.prop.path" defaultValue="ERROR"/>
<appender name="sentry" class="io.sentry.logback.SentryAppender">
<filter class="ch.qos.logback.classic.filter.ThresholdFilter">
<level>${sentryLevel}</level>
</filter>
</appender>
</configuration>
您可以通过更改 application.properties 或 application.yml 来覆盖它。
your.prop.path=INFO