在 Filebeat 中过滤日志,常见方式主要有 include / exclude 规则、processors 处理、以及 条件判断。下面按实际使用场景给你一个清晰总结。
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/*.log
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/*
exclude_files: ['.gz$', 'error.log$']
✅ 适合:
processors + drop_event(推荐)processors:
- drop_event:
when:
regexp:
message: "healthcheck|ping"
processors:
- drop_event:
when:
not:
regexp:
message: "ERROR|WARN"
processors:
- drop_event:
when:
equals:
level: "DEBUG"
processors:
- drop_event:
when:
range:
message:
lt: 10
filebeat.inputs:
- type: log
ignore_older: 24h
✅ 常用于:
processors:
- drop_event:
when:
and:
- regexp:
message: "ERROR"
- equals:
fields.env: "test"
output.elasticsearch:
hosts: ["localhost:9200"]
indices:
- index: "error-log-%{+yyyy.MM.dd}"
when.contains:
message: "ERROR"
⚠️ 这种方式只是控制输出,日志仍然会被采集
processors:
- drop_event:
when:
not:
regexp:
message: "ERROR"
processors:
- drop_event:
when:
regexp:
message: "GET /health"
processors:
- drop_event:
when:
equals:
level: "DEBUG"
✅ Filebeat 不能修改日志内容里的字段顺序
✅ drop_event 是在内存中丢弃,不会发送到 ES / Kafka
✅ 正则性能敏感,避免过于复杂
| 需求 | 推荐方式 |
|---|---|
| 忽略某些文件 | exclude_files |
| 忽略旧日志 | ignore_older |
| 按内容过滤 | processors.drop_event |
| 按环境/标签 | fields + when |
如果你愿意,可以告诉我:
我可以直接帮你写一份 可用的 filebeat.yml ✅