SpringBoot 2.x 整合Logstash记录JSON格式日志

季城
2023-12-01
  • 认真是一种态度,坚持是一种品格,优秀是一种习惯!
        都说选择难,其实坚持才难。趁着知识点还是热的,过来记录一波。为方便ELK日志收集和数据分析需要将日志修改为记录JSON格式。为此有了今天这个笔记,记录一波,方便自己后面好查。

一、基础知识

没有去百度,所以下次补全Logback及Logstash相关基础概念,如有想法自行百度一下

二、项目导包与配置

2.1 基础框架

SpringBoot 2.0.6、JDK 1.8、logback 1.2.3、logstash-logback-encoder 5.3

2.2 maven pom引包

       <dependency>
            <groupId>ch.qos.logback</groupId>
            <artifactId>logback-classic</artifactId>
            <version>1.2.3</version>
        </dependency>
        <dependency>
            <groupId>net.logstash.logback</groupId>
            <artifactId>logstash-logback-encoder</artifactId>
            <version>5.3</version>
        </dependency>

2.3 添加logback.xml配置


<?xml version="1.0" encoding="UTF-8"?>
<configuration debug="false">
    <property name="LOG_HOME" value="./logs" />
   <!-- 根据需要自行配置 -->
    <property name="APP_NAME" value="app_name" />
    <!-- 控制台输出 -->
    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n</pattern>
        </encoder>
    </appender>
    <appender name="LOGSTASH" class="ch.qos.logback.core.rolling.RollingFileAppender">
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}-%d{yyyy-MM-dd}.log</fileNamePattern>
        </rollingPolicy>
    </appender>
    <!-- 日志输出级别 -->
    <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="LOGSTASH" />
    </root>
</configuration>

2.4 JSON格式日志输出效果:

{"@timestamp":"2019-05-23T22:26:33.447+08:00","@version":"1","message":"Mapped \"{[/actuator],methods=[GET],produces=[application/vnd.spring-boot.actuator.v2+json || application/json]}\" onto protected java.util.Map<java.lang.String, java.util.Map<java.lang.String, org.springframework.boot.actuate.endpoint.web.Link>> org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping.links(javax.servlet.http.HttpServletRequest,javax.servlet.http.HttpServletResponse)","logger_name":"org.springframework.boot.actuate.endpoint.web.servlet.WebMvcEndpointHandlerMapping","thread_name":"main","level":"INFO","level_value":20000}
{"@timestamp":"2019-05-23T22:26:33.620+08:00","@version":"1","message":"Registering beans for JMX exposure on startup","logger_name":"org.springframework.jmx.export.annotation.AnnotationMBeanExporter","thread_name":"main","level":"INFO","level_value":20000}
{"@timestamp":"2019-05-23T22:26:33.622+08:00","@version":"1","message":"Bean with name 'dataSource' has been autodetected for JMX exposure","logger_name":"org.springframework.jmx.export.annotation.AnnotationMBeanExporter","thread_name":"main","level":"INFO","level_value":20000}
{"@timestamp":"2019-05-23T22:26:33.631+08:00","@version":"1","message":"Located MBean 'dataSource': registering with JMX server as MBean [com.zaxxer.hikari:name=dataSource,type=HikariDataSource]","logger_name":"org.springframework.jmx.export.annotation.AnnotationMBeanExporter","thread_name":"main","level":"INFO","level_value":20000}

2.5 直接输出到Logstash

以上为输出到文件的Logstash配置方式,当然也只支持直接输出到Logstash,如下配置:


<appender name="TCP_LOGSTASH"  class="net.logstash.logback.appender.LogstashTcpSocketAppender">
         <destination>IP地址:端口号</destination>
        <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <fileNamePattern>${LOG_HOME}/${APP_NAME}--info-%d{yyyy-MM-dd}.log</fileNamePattern>
            <filter class="ch.qos.logback.classic.filter.LevelFilter">
                 <level>INFO</level>
                <onMatch>ACCEPT</onMatch>
                <onMismatch>DENY</onMismatch>
            </filter>
        </rollingPolicy>
    </appender>

这里就不讲Logstash安装和使用(我也不是那么熟<_>!),可参考官网,附上下载地址,Logstash的使用可参考Logstash 最佳实践

 类似资料: