第1张

目录
  • 一、Logback扩展点介绍
    • 1、为什么不能用logback.xml配置文件呢?
    • 2、扩展点缺点
  • 二、指定Profile配置
  • 三、环境属性

一、Logback扩展点介绍

Spring Boot对Logback做了一些扩展,通过这些拓扩点我们可以用一些更高级的配置,这些拓展点我们可以在logback-spring.xml配置文件中使用。

1、为什么不能用logback.xml配置文件呢?

因为标准的logback.xml配置文件加载时间过早,所以不能用相关的扩展点。所以我们需要用logback-spring.xml配置文件或者定义logging.config属性指定配置文件位置。

2、扩展点缺点

扩展点不能结合Logback的配置文件自动扫描一起使用。如果配置文件有更改,会出现如下相关的错误日志。

ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for
[springProperty], current ElementPath is [[configuration][springProperty]]
ERROR in ch.qos.logback.core.joran.spi.Interpreter@4:71 - no applicable action for
[springProfile], current ElementPath is [[configuration][springProfile]]



二、指定Profile配置

通过标签我们可以基于激活的Spring Boot Profile文件可选地包含或者排除配置段。标签可以在元素中的任何地方使用。通过name属性可以指定哪个profile生效。

标签可以包含profile名称或者profile表达式,profile表达式可以指定更复杂的逻辑,例如production & (eu-central | eu-west)。详情请参考:profile表达式。该标签的具体用例如下:

<springProfile name="staging">
 <!-- 当spring.profiles.active=staging时配置才生效 -->
</springProfile>

<springProfile name="dev | staging">
 <!-- 当spring.profiles.active=dev或者staging时配置才生效
-->
</springProfile>

<springProfile name="!production">
 <!-- 当spring.profiles.active!=production时才生效 -->
</springProfile>


三、环境属性

通过标签可以在logback-spring.xml配置文件中访问Spring环境变量,如从application.properties或者application.yml文件中访问相关属性。

该标签和Logback中的作用一致,在标签中定义属性值不是直接指定value属性,而是指定source属性(从环境变量中读取指定属性的属性值)。

如果环境变量中没有指定属性值,可以通过defaultValue属性指定默认值。示例如下:

<springProperty scope="context" name="fluentHost" source="myapp.fluentd.host" defaultValue="localhost"/>
 
<appender name="FLUENT" class="ch.qos.logback.more.appenders.DataFluentAppender">
 <remoteHost>${fluentHost}</remoteHost>
 ...
</appender>

备注:source属性格式必须是kebab格式(即:my.property-name)。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/920481.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-16
下一篇 2022-05-16

发表评论

登录后才能评论

评论列表(0条)

保存