在Debian系統下,要自定義Filebeat的日志輸出格式,你需要修改Filebeat的配置文件。以下是具體步驟:
/etc/filebeat/filebeat.yml
。使用文本編輯器打開它,例如使用nano編輯器:sudo nano /etc/filebeat/filebeat.yml
filebeat.yml
文件中,找到output.elasticsearch
部分。如果你還沒有配置輸出到Elasticsearch,請添加以下內容:output.elasticsearch:
hosts: ["localhost:9200"]
將localhost:9200
替換為你的Elasticsearch實例的地址和端口。
output.elasticsearch
部分下,添加fields_under_root
設置,將其設置為true
。這將使Filebeat將所有事件字段放在Elasticsearch文檔的根級別,而不是嵌套在fields
字段中。output.elasticsearch:
hosts: ["localhost:9200"]
fields_under_root: true
filebeat.yml
文件中,找到fields
部分。在這里,你可以添加自定義字段,這些字段將附加到每個事件中。例如:fields:
custom_field1: "value1"
custom_field2: "value2"
保存并關閉filebeat.yml
文件。
重新啟動Filebeat服務以應用更改:
sudo systemctl restart filebeat
現在,Filebeat將以自定義的格式輸出日志。你可以在Elasticsearch中查看事件,看到自定義字段已添加到文檔中。