springboot logback 从apollo配置中心读取变量
1、在apollo配置中心添加
logback-config.properties配置文件
2、项目的application.yml配置文件配置如下本文来源gao@!dai!ma.com搞$$代^@码5网@
主要是eagerLoad.enabled: true这个配置
app: id: SX-sale-app-soa apollo: bootstrap: enabled: true #将Apollo配置加载提到初始化日志系统之前 eagerLoad: enabled: true namespaces: application.yml,logback-config
3、在logback.xml配置springProperty标签
设置好标签名称和配置中心变量名称的,使用的时候${name}引入该变量
<?xml version="1.0" encoding="UTF-8"?> <!-- 日志级别从低到高分为TRACE < DEBUG < INFO < WARN < ERROR < FATAL,如果设置为WARN,则低于WARN的信息都不会输出 --> <!-- scan:当此属性设置为true时,配置文件如果发生改变,将会被重新加载,默认值为true --> <!-- scanPeriod:设置监测配置文件是否有修改的时间间隔,如果没有给出时间单位,默认单位是毫秒。当scan为true时,此属性生效。默认的时间间隔为1分钟。 --> <!-- debug:当此属性设置为true时,将打印出logback内部日志信息,实时查看logback运行状态。默认值为false。 --> <configuration scan="true" scanPeriod="10 seconds"> <!-- 读取apollo配置中心设置的变量 --> <springProperty scope="context" name="logstash.host" source="logstash.host"></springProperty> <springProperty scope="context" name="logstash.port" source="logstash.port"></springProperty> <springProperty scope="context" name="log.path" source="log.path"></springProperty> <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <remoteHost>${logstash.host}</remoteHost> <port>${logstash.port}</port> <!-- encoder必须配置,有多种可选 --> <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" > <!-- "appname":"yang_test" 的作用是指定创建索引的名字时用,并且在生成的文档中会多了这个字段 --> <customFields>{"appname":"server-user"}</customFields> </encoder> </appender>
这个是后启动日志中会报如下错误:
20:26:50,683 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@120:31 – no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]
20:26:50,683 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@121:58 – no applicable action for [logger], current ElementPath is [[configuration][springProfile][logger]]
20:26:50,683 |-ERROR in ch.qos.logback.core.joran.spi.Interpreter@132:42 – no applicable action for [appender-ref], current ElementPath is [[configuration][springProfile][logger][appender-ref]]