文章轉載自:https://blog.csdn.net/UbuntuTouch/article/details/106688240
生產一個叫做 json_logs 的文件:
{"user_name": "arthur", "id": 42, "verified": false, "event": "logged_in"}
{"user_name": "arthur", "id": 42, "verified": true, "event": "changed_state"}
這種 JSON 格式的文件人眼難以理解,但是具有的優點是,數據已經按照 Elasticsearch 喜歡的格式進行了結構化。
Filebeat 是一個用Go語言編寫的開源日志傳送器,可以將日志行發送到Logstash和Elasticsearch。 它提供了“至少一次”保證的數據傳輸,因此你永遠不會丟失日志行,並且它使用了背壓敏感協議,因此不會使你的管道過載。 還包括基本過濾和多行關聯。
如果你的日志就像上面的示例一樣,Filebeat 每行存儲一個JSON對象,它還可以本地解碼 JSON 對象。
這是一個示例配置文件,該文件配置Filebeat來拾取文件並將 JSON 對象發送到 Elasticsearch:
filebeat_json.yml
filebeat.inputs:
- type: log
enabled: true
tags: ["i", "love", "json"]
json.message_key: event
json.keys_under_root: true
json.add_error_key: true
fields:
planet: liuxg
paths:
- /Users/liuxg/python/logs/json_logs
output.elasticsearch:
hosts: ["localhost:9200"]
index: "json_logs1"
setup.ilm.enabled: false
setup.template.name: json_logs1
setup.template.pattern: json_logs1
在運行 Filebeat 之前,我們可以在 Kibana 中執行如下的命令:
PUT json_logs1
{
"mappings": {
"properties": {
"event": {
"type": "keyword"
},
"id": {
"type": "long"
},
"user_name": {
"type": "keyword"
},
"verified": {
"type": "boolean"
}
}
}
}
在這里,我們定義這個索引的 mapping。
我們接着執行運行 Filebeat:
./filebeat -e -c ~/python/logs/filebeat_json.yml
那么在我們的 Kibana 中,我們可以查詢到最新生成的文檔:
GET json_logs1/_search
{
"took" : 0,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 2,
"relation" : "eq"
},
"max_score" : 1.0,
"hits" : [
{
"_index" : "json_logs1",
"_type" : "_doc",
"_id" : "uw-jonIBB4HethT_mOIZ",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2020-06-11T09:08:48.550Z",
"user_name" : "arthur",
"verified" : false,
"tags" : [
"i",
"love",
"json"
],
"fields" : {
"planet" : "liuxg"
},
"ecs" : {
"version" : "1.5.0"
},
"agent" : {
"ephemeral_id" : "0c9b96dd-76c8-45c5-96ef-00859f9e12dc",
"hostname" : "liuxg",
"id" : "be15712c-94be-41f4-9974-0b049dc95750",
"version" : "7.7.0",
"type" : "filebeat"
},
"id" : 42,
"event" : "logged_in",
"log" : {
"offset" : 0,
"file" : {
"path" : "/Users/liuxg/python/logs/json_logs"
}
},
"input" : {
"type" : "log"
},
"host" : {
"name" : "liuxg"
}
}
},
{
"_index" : "json_logs1",
"_type" : "_doc",
"_id" : "vA-jonIBB4HethT_mOIZ",
"_score" : 1.0,
"_source" : {
"@timestamp" : "2020-06-11T09:08:48.551Z",
"event" : "changed_state",
"input" : {
"type" : "log"
},
"log" : {
"offset" : 75,
"file" : {
"path" : "/Users/liuxg/python/logs/json_logs"
}
},
"user_name" : "arthur",
"id" : 42,
"verified" : true,
"tags" : [
"i",
"love",
"json"
],
"fields" : {
"planet" : "liuxg"
},
"ecs" : {
"version" : "1.5.0"
},
"host" : {
"name" : "liuxg"
},
"agent" : {
"version" : "7.7.0",
"type" : "filebeat",
"ephemeral_id" : "0c9b96dd-76c8-45c5-96ef-00859f9e12dc",
"hostname" : "liuxg",
"id" : "be15712c-94be-41f4-9974-0b049dc95750"
}
}
}
]
}
}
如你所見,Filebeat 自動添加一個時間戳。 請注意,這是讀取日志行的時間,可能與應用程序寫入日志行的時間不同。 如果需要更好的准確性,可以設置 structlog 庫以生成時間戳。
Filebeat 還會自動添加一些元數據(例如主機名),並使其易於通過配置文件添加自定義字段和標簽。 這意味着應用程序不必擔心從環境中添加元數據。