在 CentOS 上使用 Filebeat 过滤数据,通常是通过 Filebeat 自身的 processors 或结合 Logstash / Elasticsearch Ingest Pipeline 来实现。下面按常见场景给你说明。
Filebeat 支持在 filebeat.yml 中使用 processors 对数据进行过滤、裁剪、丢弃。
例如:过滤掉包含 DEBUG 的日志
processors:
- drop_event:
when:
contains:
message: "DEBUG"
processors:
- drop_fields:
fields: ["agent.version", "ecs.version"]
ignore_missing: true
processors:
- drop_event:
when:
regexp:
message: "^test.*"
processors:
- drop_event:
when:
and:
- contains:
message: "error"
- equals:
log.level: "debug"
filebeat.inputs:
- type: log
paths:
- /var/log/app/*.log
tags: ["app"]
- type: log
paths:
- /var/log/nginx/*.log
tags: ["nginx"]
在 processors 中按 tag 过滤:
processors:
- drop_event:
when:
contains:
tags: "nginx"
如果是 JSON 日志:
filebeat.inputs:
- type: log
paths:
- /var/log/app.json
json.keys_under_root: true
json.add_error_key: true
然后按字段过滤:
processors:
- drop_event:
when:
equals:
status: 200
如果过滤逻辑复杂,推荐:
Filebeat → Logstash → ES
filter {
if [message] =~ "DEBUG" {
drop {}
}
}
vi /etc/filebeat/filebeat.yml
filebeat test config
systemctl restart filebeat
systemctl status filebeat
❌ 在 Elasticsearch 里过滤(太晚)
✅ 在 Filebeat / Logstash 里过滤(更早、省资源)
如果你能给我:
我可以直接帮你写 可用的 filebeat.yml 配置。