LOGSTASH与logback整合
项目pom文件添加依赖
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>7.0.1</version>
</dependency>
logback.xml添加
<appender name="LOGSTASH" class="net.logstash.logback.appender.LogstashTcpSocketAppender">
<destination>localhost:9527</destination>
<!-- encoder必须配置,有多种可选 -->
<encoder charset="UTF-8" class="net.logstash.logback.encoder.LogstashEncoder">
<customFields>{"app_name":"csmf_mall"}</customFields>
</encoder>
</appender>
root标签中添加
<appender-ref ref="LOGSTASH"/>
说明
destination: logback日志输入的目标地址
LOGSTASH安装
logstash下载地址:https://www.elastic.co/cn/downloads/logstash
安装参考: https://www.cnblogs.com/cpy-devops/p/9287531.html
修改config/logstash.yml
#开启配置文件热加载
config.reload.automatic: true
配置字段说明:https://segmentfault.com/a/1190000016591476
修改config/logstash-sample.conf
input {
tcp {
# 在9527端口接收logback传来的日志
host => "0.0.0.0"
port => 9527
mode => "server"
tags => ["tags"]
codec => json_lines
}
}
filter {
}
output {
#控制台输出
stdout { codec => rubydebug }
#输出到es
elasticsearch {
hosts => ["127.0.0.1:9200"]
#user => "user"
#password => "password"
index => "csmfmall_provider-%{+YYYY.MM.dd}"
}
}
如果需要过滤日志输出,需要在filter下配置。
input、filter、output插件说明:https://www.elastic.co/guide/en/logstash/current/index.html
启动logstash
./bin/logstash -f ./config/logstash-simple.conf
按上面启动方式启动后,可在控制台日志输出
nohup ./bin/logstash -f ./config/logstash-simple.conf >/dev/null 2>log &
上面方式为后台启动方式
作者:idanyang
出处:http://www.cnblogs.com/idanyang/
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。