知乎專欄 | 多維度架構 | | | 微信號 netkiller-ebook | | | QQ群:128659835 請註明“讀者” |
將 Spring boot 日誌寫入 ELK 有多種實現方式,這裡僅提供三種方案:
Spring boot -> logback -> Tcp/IP -> logstash -> elasticsearch
這種方式實現非常方便不需要而外包或者軟件Spring boot -> logback -> Redis -> logstash -> elasticsearch
利用 Redis 提供的發佈訂閲功能將日誌投遞到 elasticsearchSpring boot -> logback -> Kafka -> logstash -> elasticsearch
Kafka 方法適合大數據的情況。logstash 配置
input { tcp { host => "172.16.1.16" port => 9250 mode => "server" tags => ["tags"] codec => json_lines //可能需要更新logstash插件 } } output { stdout{codec =>rubydebug} elasticsearch { hosts => ["localhost:9200"] //這塊配置需要帶連接埠號 flush_size => 1000 } }
Spring boot logback.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property resource="properties/logback-variables.properties" /> <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> <encoder charset="UTF-8"> <pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n </pattern> </encoder> </appender> <appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender"> <destination>172.16.1.16:9250</destination> <encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder" /> </appender> <!--<appender name="async" class="ch.qos.logback.classic.AsyncAppender">--> <!--<appender-ref ref="stash" />--> <!--</appender>--> <root level="info"> <!-- 設置日誌級別 --> <appender-ref ref="STDOUT" /> <appender-ref ref="LOGSTASH" /> </root> </configuration>
https://github.com/kmtong/logback-redis-appender
Maven pom.xml 增加 Logback Redis 依賴
<!-- https://mvnrepository.com/artifact/com.cwbase/logback-redis-appender --> <dependency> <groupId>com.cwbase</groupId> <artifactId>logback-redis-appender</artifactId> <version>1.1.5</version> </dependency>
Spring boot logback.xml 配置
<?xml version="1.0" encoding="UTF-8"?> <configuration> <include resource="org/springframework/boot/logging/logback/defaults.xml" /> <include resource="org/springframework/boot/logging/logback/file-appender.xml" /> <property name="type.name" value="test" /> <appender name="LOGSTASH" class="com.cwbase.logback.RedisAppender"> <source>spring-application</source> <type>${type.name}</type> <host>localhost</host> <key>logstash:redis</key> <tags>test-2</tags> <mdc>true</mdc> <location>true</location> <callerStackIndex>0</callerStackIndex> <!--additionalField添加附加欄位 用於head插件顯示 --> <additionalField> <key>MyKey</key> <value>MyValue</value> </additionalField> <additionalField> <key>MySecondKey</key> <value>MyOtherValue</value> </additionalField> </appender> <root level="INFO"> <appender-ref ref="FILE" /> <appender-ref ref="LOGSTASH" /> </root> </configuration>
logstash 配置
input { redis { host => 'localhost' data_type => 'list' port => "6379" key => 'logstash:redis' #自定義 type => 'redis-input' #自定義 } } output { elasticsearch { host => "localhost" codec => "json" protocol => "http" } }