在 CentOS 上使用 Filebeat 过滤日志,常见做法是通过 Filebeat 自身的配置 或 结合 Elasticsearch / Logstash 来实现。下面按由简单到进阶的方式说明。
include_lines / exclude_lines(最常用)适用于:按行内容过滤
ERROR 的日志filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
include_lines: ['ERROR']
DEBUG 的日志 exclude_lines: ['DEBUG']
✅ 特点:
exclude_files(按文件名过滤)适用于:忽略某些日志文件
filebeat.inputs:
- type: log
paths:
- /var/log/app/*.log
exclude_files: ['\.gz$', 'test\.log']
✅ 常用于忽略:
processors 过滤(推荐进阶方式)适用于:按字段、标签、条件删除或保留事件
message 中包含 health check 的日志processors:
- drop_event:
when:
regexp:
message: "health check"
level 为 ERROR 的日志processors:
- drop_event:
when:
not:
equals:
level: "ERROR"
✅ 特点:
fields + 条件过滤(分类日志)适用于:给日志打标签,后续在 ES / Kibana 中过滤
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log
fields:
log_type: nginx_access
fields_under_root: true
然后在 Kibana 中:
log_type: nginx_access
如果你使用 Filebeat → Logstash → ES,可以在 Logstash 中过滤。
filter {
if "ERROR" not in [message] {
drop {}
}
}
✅ 适合:
# 配置文件
/etc/filebeat/filebeat.yml
# 重启服务
systemctl restart filebeat
# 查看状态
systemctl status filebeat
# 测试配置
filebeat test config
| 需求 | 推荐方式 |
|---|---|
| 简单按关键字过滤 | include_lines / exclude_lines |
| 删除某些日志 | processors.drop_event |
| 忽略某些文件 | exclude_files |
| 复杂解析 | Logstash |
| 只做分类 | fields |
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/app/*.log
exclude_lines: ['DEBUG']
processors:
- drop_event:
when:
regexp:
message: "health check"
output.elasticsearch:
hosts: ["localhost:9200"]
如果你愿意,可以告诉我:
我可以帮你写一份可直接使用的 Filebeat 配置。