(八)Spring Boot中对Logback的拓展

一、Logback扩展点介绍

Spring BootLogback做了一些扩展,通过这些拓扩点我们可以用一些更高级的配置,这些拓展点我们可以在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配置

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

<springProfile>标签可以包含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>


三、环境属性

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

该标签和Logback中的<property>作用一致,在<springProperty>标签中定义属性值不是直接指定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)。

在这里插入图片描述

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Nick说说前后端

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值