Spring整合Rabbitmq收集Logback日誌,利用進行Logstash資料整理儲存到Elasticsearch中
阿新 • • 發佈:2018-12-30
專案中我們常用的是把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 ====>從訊息佇列讀日誌
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:\home\logs\boot\info.log","E:\home\logs\boot\error.log","E:\home\logs\boot\trace.log","E:\home\logs\boot\warn.log"]
}
}
output {
elasticsearch {
hosts => ["localhost:9200"]
index => "bootsis_log"
}
}
以上配置完成,啟動rabbitmq,logstash(logstash -f logstash-rabbitmq.conf),elasticsearch,elasticsearch-head,springboot專案。
控制檯一直在列印日誌
rabbitmq訊息狀態
後期也可以整合Kinbana
微信公眾號:
JAVA程式猿成長之路
分享學習資源,學習方法,記錄程式設計師生活。