【ELFK】将filebeat收集到的json格式的日志传到logtash格式化存储到elastichsearch在kibana中展现
前言
期望要将json日志进行格式化存储到es中,显示每个json的field属性
实战
以nignx日志为例
先将nginx日志格式化
log_format cloudbaseui '{"time": "$time_local", '
'"remote_addr": "$remote_addr", '
'"remote_user": "$remote_user", '
'"request": "$request", '
'"status": "$status", '
'"body_bytes_sent": "$body_bytes_sent", '
'"http_referer": "$http_referer", '
'"http_user_agent": "$http_user_agent", '
'"http_x_forwarded_for": "$http_x_forwarded_for", '
'"request_time": "$request_time", '
'"request_length": "$request_length", '
'"HTTP_VIA": "$http_via", '
'"host": "$http_host" ,'
'"http_cookie": "$http_cookie"}';
filebeat收集日志+传到logstash
#web
- type: log
enabled: true
paths:
- /var/log/web/logs/access.log
fields:
tag: test-web-access
#传到logtash
output.logstash:
# The Logstash hosts
hosts: ["logstash1:15044"]
logtash读取json
- 第一种方式是使用filter json原始记录被保存,同时字段也被解析保存(推荐)
if "test-web-access" in [fields][tag] {
json {
source => "message"
#target => "doc"
# #remove_field => ["message"]
}
}
- 第二种使用codec => json方式字段被解析,原始记录内容也没有保存
if "test-web-access" in [fields][tag] {
codec => json {
charset => "UTF-8"
}
}
- 第三种方式是直接设置format => json,与第二种方式相同,字段被解析,原始记录内容也没有保存
if "test-web-access" in [fields][tag] {
format => json
}
该博客介绍了如何使用ELK Stack(Elasticsearch, Logstash, Kibana)处理JSON格式的日志。首先,通过在Nginx配置中定义log_format将日志转换为JSON格式。接着,Filebeat配置收集这些日志并发送到Logstash。在Logstash中,可以通过三种方式解析JSON日志:使用filter的json插件、codec=json和format=json。每种方法都有其特点,如原始记录保存与否。最后,解析后的日志将存储在Elasticsearch中,并在Kibana中展示。
1485

被折叠的 条评论
为什么被折叠?



