【Spring Boot】Spring Boot之利用Logstash将日志转换成以JSON的格式存储和输出
一、Logstash的作用
Logstash是一个完全开源的工具,它可以对日志进行收集、过滤,能非常方便地将日志转换成以JSON的格式存储和输出,并将其存储供以后使用。
二、整合Logstash的步骤
注意:需要在logback日志框架的基础上:https://www.cnblogs.com/756623607-zhang/p/9695131.html
1)增加pom依赖
<!--logstash-->
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.2</version>
</dependency>
2)logback配置文件增加Logstash的appender
<!-- LOGSTASH --> <appender name="FILE-LOGSTASH" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${logback.path}/${application.name}-logstash.log</file> <append>true</append> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>${logback.path}/${application.name}-logstash.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <maxHistory>${logback.max-history}</maxHistory> <maxFileSize>${logback.max-file-size}</maxFileSize> </rollingPolicy> <encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder"> <providers> <timestamp> <timeZone>UTC</timeZone> </timestamp> <pattern> <pattern> { "severity": "%level", "service": "${springAppName:-}", "trace": "%X{X-B3-TraceId:-}", "span": "%X{X-B3-SpanId:-}", "exportable": "%X{X-Span-Export:-}", "pid": "${PID:-}", "thread": "%thread", "class": "%logger{40}", "rest": "%message" } </pattern> </pattern> </providers> </encoder> </appender>
3)引用该appender
<root level="${logback.root-level}"> <appender-ref ref="FILE-LOGSTASH"/> </root>
你投入得越多,就能得到越多得价值