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

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

       
            ch.qos.logback
            logback-classic
            1.2.3
        
        
            net.logstash.logback
            logstash-logback-encoder
            5.3
        

2.3 添加logback.xml配置




    
   
    
    
    
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
        
    
    
        
        
            ${LOG_HOME}/${APP_NAME}-%d{yyyy-MM-dd}.log
        
    
    
    
        
        
    

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> 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,如下配置:



         IP地址:端口号
        
        
            ${LOG_HOME}/${APP_NAME}--info-%d{yyyy-MM-dd}.log
            
                 INFO
                ACCEPT
                DENY
            
        
    

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

你可能感兴趣的:(坚持学习第二天:SpringBoot 2.x 整合Logstash记录JSON格式日志)