Ubuntu 上 Filebeat 自定义标签指南
可行性与作用 在 Ubuntu 上的 Filebeat 支持通过配置为事件添加自定义 tags 与 fields,用于后续在 Elasticsearch/Kibana 或 Logstash 中做过滤、路由与聚合。常见做法是给不同日志路径设置不同标签,便于按标签分流或创建索引。
配置方式
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/myapp/*.log
tags: ["myapp", "production"]
fields:
app_name: "myapp"
env: "prod"
fields_under_root: false # 为 true 时将 fields 提升到事件根层级
# 使用处理器添加/修改标签与字段
processors:
- add_fields:
target: ""
fields:
processed: true
- decode_json_fields:
fields: ["message"]
target: "json_content"
overwrite_keys: true
要点:tags 是字符串数组;fields 是键值对;fields_under_root 控制是否放在根层级。按标签分流示例
input {
redis {
host => "192.168.3.65"
port => 46379
password => "password"
db => 3
data_type => "list"
key => "0388"
}
}
output {
if "psych-log-0388" in [tags] {
elasticsearch {
hosts => ["192.168.3.59:9200"]
index => "psych-log-0388-%{+YYYY.MM.dd}"
}
}
if "bert-log-0388" in [tags] {
elasticsearch {
hosts => ["192.168.3.59:9200"]
index => "bert-log-0388-%{+YYYY.MM.dd}"
}
}
}
要点:在 Filebeat 侧给不同日志路径分别打上如 psych-log-0388、bert-log-0388 的标签,Logstash 即可据此分流到不同索引。验证与生效
sudo filebeat test config -e
sudo filebeat -e -c /etc/filebeat/filebeat.yml
建议先用控制台输出(如 output.console)验证事件是否携带期望的 tags/fields,再切回生产输出。修改后重启服务:sudo systemctl restart filebeat
sudo systemctl enable filebeat
要点:test config 检查语法;-e 输出到控制台便于调试;重启使配置生效。