在 CentOS 下使用 Filebeat 过滤日志,通常是通过 Filebeat 配置(filebeat.yml) 中的 processors 或 include_lines / exclude_lines 来实现的。下面按常见场景给你一个完整说明。
include_lines / exclude_lines(最常用)适合:按关键字过滤整行日志
ERROR 的日志filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
include_lines: ['ERROR']
DEBUG 的日志filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
exclude_lines: ['DEBUG']
✅ 支持 正则表达式
exclude_lines: ['^INFO', '^DEBUG']
processors(更强大,推荐)适合:
示例:丢弃包含 health check 的日志
processors:
- drop_event:
when:
regexp:
message: "health check"
processors:
- drop_event:
when:
not:
regexp:
message: "ERROR"
processors:
- drop_fields:
fields: ["ecs", "agent", "host"]
ignore_missing: true
processors:
- drop_event:
when:
and:
- regexp:
message: "ERROR"
- contains:
message: "timeout"
如果日志是 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:
level: "DEBUG"
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
exclude_lines: ['200']
processors:
- drop_event:
when:
regexp:
message: "health check"
output.elasticsearch:
hosts: ["localhost:9200"]
systemctl restart filebeat
systemctl status filebeat
✅ 先用 stdout 测试,避免误删数据:
output.console:
pretty: true
确认过滤正确后再切回 Elasticsearch / Logstash。
| 需求 | 推荐方式 |
|---|---|
| 简单行过滤 | include_lines / exclude_lines |
| 复杂条件 | processors + drop_event |
| JSON 日志 | json 解析 + processors |
| 减少数据量 | drop_fields |
如果你愿意,可以把 你的日志样例 + 期望过滤规则 发出来,我可以直接帮你写一份可用的 filebeat.yml ✅