在 CentOS 上使用 Filebeat 过滤日志,常见做法有 3 种,按使用频率和复杂度从简单到高级说明:
include_lines / exclude_lines(最基础)适合:按行内容简单过滤
filebeat.inputs:
- type: log
paths:
- /var/log/app/*.log
include_lines: ['ERROR']
exclude_lines: ['DEBUG']
✅ 优点:简单
❌ 缺点:只能按行匹配,不能做复杂逻辑
processors(推荐,官方主推)适合:字段过滤、丢弃日志、添加字段
processors:
- drop_event.when.contains:
message: "health check"
processors:
- drop_event.when.regexp:
message: "^DEBUG"
processors:
- drop_event.when.not.or:
- contains:
message: "ERROR"
- contains:
message: "WARN"
processors:
- drop_fields:
fields: ["agent.version", "ecs.version"]
✅ 优点:灵活、性能好
✅ 推荐在 CentOS 生产环境使用
fields / tags + Logstash(复杂场景)适合:需要复杂解析、多条件过滤
filebeat.inputs:
- type: log
paths:
- /var/log/nginx/access.log
tags: ["nginx"]
filter {
if "nginx" in [tags] {
grok {
match => { "message" => "%{COMBINEDAPACHELOG}" }
}
}
}
vi /etc/filebeat/filebeat.yml
filebeat test config
systemctl restart filebeat
systemctl status filebeat
| 需求 | 方式 |
|---|---|
| 不要 DEBUG | exclude_lines |
| 只要 ERROR | include_lines |
| 丢弃某类日志 | drop_event |
| 修改字段 | add_fields |
| 多条件过滤 | when.or / when.and |
如果你能告诉我:
我可以直接帮你写一份 可用的 filebeat.yml 配置。