在 Ubuntu 上使用 Filebeat 时,可以通过 filebeat.inputs 配置中的 exclude_files 或 prospector 相关排除选项 来排除不需要采集的文件。
下面按常见场景说明。
exclude_files 排除文件(最常用)exclude_files 支持 正则表达式,匹配的是文件路径。
.gz 和 .swp 文件filebeat.inputs:
- type: log
enabled: true
paths:
- /var/log/myapp/*.log
exclude_files: ['.gz$', '.swp$']
filebeat.inputs:
- type: log
paths:
- /var/log/**/*.log
exclude_files: ['/var/log/debug/']
如果某个目录完全不需要:
filebeat.inputs:
- type: log
paths:
- /var/log/**/*.log
exclude_files: ['/var/log/old/.*']
或使用多个 input 分开管理更清晰。
exclude_files: ['^\.']
排除如 .hidden.log 这种文件。
paths:
- /var/log/myapp/*
exclude_files:
- error.log
exclude_files: ['2023\d{2}\d{2}.log$']
exclude_files: ['filebeat']
或:
paths:
- /var/log/*.log
exclude_files: ['/var/log/filebeat/']
通常配置文件:
/etc/filebeat/filebeat.yml
修改后重启:
sudo systemctl restart filebeat
检查配置:
sudo filebeat test config
exclude_files 匹配的是 完整路径(?i) 这种某些高级语法如果你有具体路径或文件类型(比如 Nginx、Docker、Java 日志),我可以帮你写一份精确配置。