Spring集成Rabbitmq收集Logback日志,利用进行Logstash数据整理存储到Elasticsearch中

项目中我们常用的是把Logback打印的日志存储到文件中保存到硬盘上,这样不利于日志的收集和分析。

以下演示在SpringBoot中通过rabbitmq收集logback日志存储到Elasticsearch中。

环境准备:安装RabbitMQ,安装Elasticsearch和Elasticsearch-Head(可视化插件),安装Logstash.====》之前博客已经介绍过了。

pom.xml

引入spring-amqp

<dependency>
    <groupId>org.springframework.amqp</groupId>
    <artifactId>spring-rabbit</artifactId>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-amqp</artifactId>
</dependency>

logback-spring.xml

<!--logback日志写入rabbitmq -->
    <appender name="RabbitMq"
        class="org.springframework.amqp.rabbit.logback.AmqpAppender">
        <layout>
            <pattern><![CDATA[ %d %p %t [%c] - <%m>%n ]]></pattern>
        </layout>
        <!--rabbitmq地址 -->
        <addresses>localhost:5672</addresses>
        <abbreviation>36</abbreviation>
        <includeCallerData>true</includeCallerData>
        <applicationId>bootsis</applicationId>
        <username>springcloud</username>
        <password>123456</password>
        <!--路邮件 -->
        <!--{applicationId}  -->
        <!--%property{applicationId}.%c.%p 收集不到日志  -->
        <!--最后改为精确匹配了  非常重要 -->
        <routingKeyPattern>bootsis</routingKeyPattern>
        <generateId>true</generateId>
        <charset>UTF-8</charset>
        <durable>true</durable>
        <deliveryMode>NON_PERSISTENT</deliveryMode>
        <declareExchange>true</declareExchange>
        <autoDelete>false</autoDelete>
    </appender>

   <root level="INFO">
        <appender-ref ref="STDOUT" />
        <appender-ref ref="INFO_FILE" />
        <appender-ref ref="WARN_FILE" />
        <appender-ref ref="ERROR_FILE" />
        <!--这块也要添加-->
        <appender-ref ref="RabbitMq" /> 
    </root>

Logstash配置

在logstash安装目录的bin目录下新建

logstash-rabbitmq.conf   ====>从消息队列读日志

配置如下,logstash整合rabbitmq详细配置见https://www.elastic.co/guide/en/logstash/current/plugins-inputs-rabbitmq.html

input { 
   rabbitmq {
       type =>"all"
       durable => true
       exchange => "logs"
	   exchange_type => "topic"
	   key => "bootsis"
	   host => "localhost"
	   port => 5672
	   user => "springcloud"
	   password => "123456"
	   queue => "boot-sis-mq"
       auto_delete => false
    
  }	
 }

output {
  elasticsearch { 
       hosts => ["localhost:9200"] 
	   index => "bootsis_log_mq"
	   }
}

额外:从文件目录读日志

input { 
   file {
     type => "server"
     path  =>["E:homelogsootinfo.log","E:homelogsooterror.log","E:homelogsoot	race.log","E:homelogsootwarn.log"]
  }
 }

output {
  elasticsearch { 
       hosts => ["localhost:9200"] 
	   index => "bootsis_log"
	   }
}

以上配置完成,启动rabbitmq,logstash(logstash -f logstash-rabbitmq.conf),elasticsearch,elasticsearch-head,springboot项目。

控制台一直在打印日志

访问http://localhost:9100/

rabbitmq消息状态 

后期也可以整合Kinbana

     

微信公众号

                          
原文地址:https://www.cnblogs.com/niugang0920/p/12188056.html