filebeat 7.10
ES 7.4.2
我想做的是使用filebeat从log文件中采集json格式的日志,并发送到ES中,并在ES中显示json日志的各字段和数据。
在filebeat.yml文件中进行相应的配置:
- type: log
enabled: true
paths:
- E:\testjson.log
json:
keys_under_root: true
overwrite_keys: true
add_error_key: true
#自定义的json日志的标识字段,该字段必须处于json的root
message_key: message
具体配置可参考相应文档json部分。
注意每条json日志只能占一行,如果换行的话会解析出错!
具体错误如下:
2020-12-14T16:01:50.789+0800 ERROR [reader_json] readjson/json.go:57 Error decoding JSON: EOF
2020-12-14T16:01:50.789+0800 DEBUG [processors] processing/processors.go:128 Fail to apply processor global{timestamp=[field=start_time, target_field=@timestamp, timezone=Asia/Shanghai, layouts=[2006-01-02 15:04:05.999]], drop_fields={"Fields":["log","host","input","agent","ecs","start_time"],"IgnoreMissing":false}}: failed to get time field start_time: key not found
错误原因:
刚开始报了上述错误,只有第一条日志发出去了,后边的日志都会报错Error decoding JSON: EOF
,打开debug日志还会发现json日志的第一个字段会“key not found”
。
原因是json格式的日志每条最后必须加逗号",",而我的日志中仅将其换行。
解决方法:
json日志的大括号后加逗号
{json日志},
{json日志},
在filebeat.yml中设置output.elasticsearch
:
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["ip:9200"]
这种设置之后,日志存入ES的index默认是"filebeat-%{[agent.version]}-%{+yyyy.MM.dd}"
。
当我按照官方文档尝试修改索引时,总是会报以下错误:
failed to publish events: temporary bulk send failure
修改的配置如下:(希望大佬指出错误原因)
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["IP:9200"]
index: "test-%{[agent.version]}-normal-%{+yyyy.MM.dd}"
setup.template.overwrite: true
setup.template.name: "test"
setup.template.pattern: "test-*"
setup.ilm.enabled: false
目前没有解决,因此先使用默认索引模板。
配合使用timestamp处理器和drop_fields处理器。
processors:
- timestamp:
# 格式化时间值 给 时间戳
field: start_time
# 使用我国东八区时间 格式化log时间
timezone: Asia/Shanghai
layouts:
# 具体日期格式格式可参考文档中的说明
- '2006-01-02 15:04:05.999'
test:
#test字段:必须能够成功解析的日期内容
- '2019-06-22 16:33:51.111'
- drop_fields:
fields: ["log","host","input","agent","ecs","start_time"]
ignore_missing: false
从start_time
字段中提取日志时间,并放入时间戳中,并在drop_fields中将该字段删掉即可。
整体配置文件如下:
filebeat.inputs:
- type: log
enabled: true
- E:\testjson.log
json:
keys_under_root: true
overwrite_keys: true
add_error_key: true
message_key: message
output.elasticsearch:
# Array of hosts to connect to.
hosts: ["192.168.73.101:9200"]
processors:
- timestamp:
# 格式化时间值 给 时间戳
field: start_time
# 使用我国东八区时间 格式化log时间
timezone: Asia/Shanghai
layouts:
- '2006-01-02 15:04:05.999'
test:
- '2019-06-22 16:33:51.111'
- drop_fields:
fields: ["log","host","input","agent","ecs","start_time"]
ignore_missing: false
原始日志:
{"start_time": "2020-12-13 10:37:01.072","type": "CsbTest","level": "INFO","message":"数据1","parameter":["6677"]},
{"start_time": "2020-12-13 10:37:01.072","type": "CsbTest","level": "INFO","message":"数据2","parameter":["12121"]},
ES存储日志:
{
"_index" : "filebeat-7.10.0-2020.12.14-000001",
"_type" : "_doc",
"_id" : "8d1FYHYBjbaP5OPrGcZL",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2020-12-14T02:37:01.072Z",
"message" : "数据2",
"parameter" : [
"6677"
],
"type" : "CsbTest",
"level" : "INFO",
"fields" : {
"log_type" : "normal"
}
}
}
https://www.elastic.co/guide/index.html
https://www.elastic.co/guide/en/beats/filebeat/7.9/processor-timestamp.html
https://blog.csdn.net/yk20091201/article/details/90756738
https://blog.csdn.net/qq_27818541/article/details/108063235
https://www.cnblogs.com/manhoo/archive/2009/06/25/1511066.html