文章目录
前言
定义日志为json格式,方便ELK的接入和查询。
Maven依赖
依赖的版本,查询下面的链接:
https://mvnrepository.com/artifact/net.logstash.logback/logstash-logback-encoder
例如:
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.0.1</version>
</dependency>
logback-spring.xml文件
在项目的resources目录下,添加文件 logback-spring.xml
注:文件名不允许是 logback.xml ,否则会报错:
no applicable action for [springProfile], current ElementPath is [[configuration][springProfile]]
使用LoggingEventCompositeJsonEncoder
json格式的编码器。
文件内容参考:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<jmxConfigurator/>
<springProperty scope="context" name="module_name" source="spring.application.name"/>
<!-- 读取apollo配置中心设置的变量 -->
<springProperty scope="context" name="version" source="app.version"/>
<springProperty scope="context" name="env" source="app.env"/>
<