坚持学习第二天:SpringBoot 2.x 整合Logstash记录JSON格式日志

https://www.jianshu.com/p/eecc573b6da0

 

  • 认真是一种态度,坚持是一种品格,优秀是一种习惯!
        都说选择难,其实坚持才难。趁着知识点还是热的,过来记录一波。为方便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 最佳实践



作者:醉枫浅墨
链接:https://www.jianshu.com/p/eecc573b6da0
来源:简书
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

hello_world!

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

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

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

打赏作者

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

抵扣说明:

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

余额充值