在 Ubuntu 上使用 Filebeat 过滤日志内容,通常有三种常见方式,分别适用于不同场景:
Filebeat 支持在 filebeat.yml 中使用 processors 对日志做过滤、丢弃、字段处理。
filebeat.inputs:
- type: log
paths:
- /var/log/app/*.log
processors:
- drop_event.when.contains:
message: "health check"
➡ 含义:日志内容包含 health check 的直接丢弃,不发送到 ES/Logstash。
processors:
- drop_event.when.not.contains:
message: "ERROR"
processors:
- drop_event.when.regexp:
message: "^DEBUG"
processors:
- include_fields:
fields: ["message", "timestamp"]
processors:
- exclude_fields:
fields: ["agent.version"]
适用于行级别简单过滤:
filebeat.inputs:
- type: log
paths:
- /var/log/app.log
exclude_lines: ['^INFO']
include_lines: ['^ERROR', '^WARN']
⚠ 注意:
exclude_lines / include_lines 只支持 正则表达式如果过滤逻辑复杂(JSON 解析、条件判断、字段拆分),建议:
Filebeat → Logstash → ES
filter {
if ([message] =~ /heartbeat/) {
drop {}
}
if [message] =~ /ERROR/ {
mutate {
add_field => { "level" => "error" }
}
}
}
sudo filebeat test config
sudo filebeat -e -c filebeat.yml -d "*"
| 场景 | 推荐方式 |
|---|---|
| 丢弃 debug / heartbeat | processors drop_event |
| 只采集 ERROR | include_lines |
| 复杂日志解析 | Logstash |
| 性能优先 | Filebeat processors |
如果你能给我:
我可以直接帮你写一份 可用的 filebeat.yml。