前言
在生產(chǎn)環(huán)境中绪抛,有時(shí)候會(huì)出現(xiàn)一些意想不到的問(wèn)題饥追,需要進(jìn)入服務(wù)器進(jìn)行日志排查纲刀。但是出于服務(wù)器安全問(wèn)題紫新,生產(chǎn)環(huán)境的服務(wù)器又不能交于每個(gè)開(kāi)發(fā)手中均蜜。故而需要一個(gè)日志系統(tǒng)用于線上日志排查。
話不多說(shuō)進(jìn)入正題--
流程圖
用戶發(fā)生請(qǐng)求產(chǎn)生日志信息芒率,Logback講日志信息發(fā)送并存儲(chǔ)到Kafka某個(gè)topic中(下面會(huì)說(shuō)配置)等待消費(fèi),logstash獲取到kafka中日志信息后囤耳,將其寫(xiě)入到es中,最終由kibana從es中進(jìn)行讀取展示偶芍。
PS:kafka不是必須的充择,也可以直接通過(guò)logstash進(jìn)行文件分析存儲(chǔ)到es,但是我們建議kafka
Logback配置
- pom.xml文件引入logback-kafka 相關(guān)jar包
<dependency>
<groupId>com.github.danielwegener</groupId>
<artifactId>logback-kafka-appender</artifactId>
<version>0.1.0</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.11</version>
</dependency>
logback-spring.xml 日志配置
日志傳輸?shù)絢afka的關(guān)鍵配置腋寨,下面配置都是簡(jiǎn)要配置聪铺,如想深入配置可參考官方文檔
<appender name="kafka-appender" class="com.github.danielwegener.logback.kafka.KafkaAppender">
<encoder class="com.github.danielwegener.logback.kafka.encoding.LayoutKafkaMessageEncoder">
<layout class="net.logstash.logback.layout.LogstashLayout" >
<includeContext>true</includeContext>
<includeCallerData>true</includeCallerData>
<customFields>{"service-name":"auth-service"}</customFields>
<fieldNames class="net.logstash.logback.fieldnames.ShortenedFieldNames"/>
</layout>
<charset>UTF-8</charset>
</encoder>
<!--kafka topic 需要與logstash配置的topic一致 -->
<topic>auth-service-log</topic>
<keyingStrategy class="com.github.danielwegener.logback.kafka.keying.HostNameKeyingStrategy" />
<deliveryStrategy class="com.github.danielwegener.logback.kafka.delivery.AsynchronousDeliveryStrategy" />
<producerConfig>bootstrap.servers=Your kafka ip:port</producerConfig>
</appender>
<root level="INFO">
<appender-ref ref="kafka-appender" />
</root>