日誌在系統中相當重要,尤爲是生產環境,一旦出現問題,首先是日誌中的錯誤信息觸發預警系統,而後經過郵件、短信甚至電話通知的方式報警給系統負責人。在排查修復問題階段,開發測試人員一般也要查看系統日誌,分析故障緣由。
git
ELK是經常使用的日誌處理系統,Spring集成LogStash很是方便,配置LogBack將日誌發送給LogStash。github
代碼文件spring |
功能要點json |
|
SpringBoot集成LogStashruby |
pom.xmlapp |
引入LogStash依賴:spring-boot-starter-loggingelasticsearch |
logback.xmltcp |
配置LogStash日誌格式ide |
|
application.ymlspring-boot |
引用logback.xml配置 |
|
啓動LogStash |
安裝包bin目錄 |
配置logstash.conf,啓動logstash -f logstash.conf |
l 代碼
Github下載:https://github.com/jextop/StarterApi/
l SpringBoot集成LogStash
1. 在pom.xml中添加LogStash依賴
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>5.1</version>
</dependency>
2. 在resouces目錄下添加logback.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/base.xml"/>
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>127.0.0.1:9600</destination>
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder"/>
</appender>
<root level="INFO">
<appender-ref ref="LOGSTASH"/>
</root>
</configuration>
3. 在application.yml中引用logback.xml配置:
logging:
config: classpath:logback.xml
l 啓動LogStash
1. 下載LogStash:https://www.elastic.co/downloads/logstash
官網慢時可用網盤:https://pan.baidu.com/s/1b-czkB8z5aL6rdxZtBmNEw 提取碼: arfb
2. 在bin目錄下添加logstash.conf:
input.tcp.host可設置本機ip
input.tcp.port設置端口
output.elasticsearch.hosts設置輸出地址
input{
tcp {
mode => "server"
host => "0.0.0.0"
port => 9600
codec => json_lines
}
}
output{
stdout{
codec => rubydebug
}
elasticsearch {
hosts => ["127.0.0.1:9200"]
}
}
3. 啓動:logstash -f logstash.conf,看到端口信息提示,運行成功
l 運行Spring項目,LogStash將收到日誌信息