Filebeat 在 Ubuntu 上的日志轮转可以通过配置 logrotate 工具来实现。以下是配置 Filebeat 日志轮转的步骤:
sudo apt-get update
sudo apt-get install logrotate
sudo nano /etc/logrotate.d/filebeat
/path/to/your/filebeat/log/filebeat.log {
daily
rotate 7
compress
missingok
notifempty
create 640 root adm
sharedscripts
postrotate
if [ -f /var/run/filebeat/filebeat.pid ]; then
/usr/share/filebeat/filebeat -e -c /etc/filebeat/filebeat.yml -d "*"
fi
endscript
}
这里的配置表示:
daily:每天轮转日志。rotate 7:保留 7 天的日志文件。compress:压缩旧的日志文件。missingok:如果日志文件不存在,不会产生错误。notifempty:如果日志文件为空,不进行轮转。create 640 root adm:创建新的日志文件,权限为 640,属主为 root,属组为 adm。sharedscripts:如果有多个日志文件匹配,只执行一次 postrotate 脚本。postrotate:轮转后执行的脚本。这里会重新启动 Filebeat 服务。保存并关闭文件。
测试 logrotate 配置是否正确:
sudo logrotate -f /etc/logrotate.d/filebeat
如果没有错误,表示配置正确。
现在,Filebeat 的日志轮转应该已经按照您的配置进行了。如果需要修改轮转策略,只需编辑 /etc/logrotate.d/filebeat 文件即可。