在 CentOS 上使用 Filebeat 过滤日志,通常是通过 Filebeat 自身的 processors 或结合 Logstash / Elasticsearch 的过滤 来完成。下面按常见场景给你说明。
Filebeat 支持在 filebeat.yml 中使用 processors 做过滤。
例如:过滤掉包含 DEBUG 的日志
filebeat.inputs:
- type: log
paths:
- /var/log/app/*.log
processors:
- drop_event.when.contains:
message: "DEBUG"
processors:
- drop_event.when.not.contains:
message: "ERROR"
processors:
- drop_event.when.not.equals:
level: "error"
processors:
- drop_event.when:
or:
- contains.message: "test"
- regexp.message: "^\\s*$"
filebeat.inputs:
- type: log
paths:
- /var/log/app/app.log
multiline.pattern: '^[0-9]{4}-[0-9]{2}-[0-9]{2}'
multiline.negate: true
multiline.match: after
filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/nginx/access.log
exclude_files: ['.gz$']
processors:
- drop_fields:
fields: ["agent.version", "ecs.version"]
processors:
- add_fields:
target: ''
fields:
env: production
如果过滤逻辑复杂,推荐在 Logstash 中做:
filter {
if [message] =~ "DEBUG" {
drop {}
}
}
filebeat test config
filebeat test output
重启 Filebeat:
systemctl restart filebeat
| 需求 | 方法 |
|---|---|
| 丢弃 DEBUG 日志 | drop_event |
| 只采集 ERROR | drop_event.when.not |
| 忽略空行 | regexp |
| 多行日志 | multiline |
| 复杂过滤 | Logstash |
如果你能告诉我:
我可以给你一份 可直接用的 filebeat.yml 配置。