Filebeat可通过配置文件(filebeat.yml
)中的多种方式实现日志过滤,以下是常见方法及对应配置示例:
按文件路径/名称过滤
通过paths
配置通配符路径,或使用exclude_files
排除特定文件(如.gz
结尾的压缩文件):
filebeat.inputs:
- type: log
paths: ["/var/log/*.log"]
exclude_files: [".gz$"] # 排除.gz文件
按日志行内容过滤
include_lines
或exclude_lines
配置正则表达式,匹配行首/行尾内容:processors:
- include_lines: ["ERROR", "WARN"] # 仅保留包含ERROR/WARN的行
- exclude_lines: ["^dbg"] # 排除以dbg开头的行
- **正则表达式过滤**:在`when`条件中使用`regexp`匹配日志内容(如特定字段或全文):
```yaml
output.elasticsearch:
when.regexp:
message: "foo.*" # 仅输出message字段匹配正则的日志
按字段值过滤
通过drop_event
或drop_fields
处理器,基于字段值或存在性丢弃日志:
processors:
- drop_event:
when:
equals:
level: "INFO" # 丢弃level字段为INFO的日志
- drop_fields:
fields: ["unnecessary_field"] # 删除指定字段
多条件组合过滤
使用and
/or
/not
逻辑组合多个过滤条件,例如:
when:
and:
- equals:
source: "/var/log/app.log"
- contains:
message: "ERROR" # 仅保留来源为app.log且包含ERROR的日志
配置完成后,重启Filebeat使规则生效:
sudo systemctl restart filebeat
更多细节可参考官方文档:Filebeat Filtering Documentation。