问题:
旧服务日志输出量较多,一段时间需要手动清理日志,日常排查问题的时候,日志量过大grep 检索耗时较长
方案:
使用系统自带应用logrotate 定时切割,压缩日志
以下是logrotate配置模板
功能:30天日志滚动,置空且按时间归档日志,保留一天日志不压缩
# vim /app/tomcat_logrotate
/app/tomcat/logs/catalina.out {
rotate 30
daily
copytruncate
compress
delaycompress
notifempty
dateext
dateformat -%Y-%m-%d-%s
missingok
olddir /app/tomcat/logs/logrotate_oldlog
}
默认 logrotate 的定时任务配置 cat /etc/cron.daily/logrotate
便于灵活修改,本文由于使用crontab 定时触发
# 建议 logrotate 配置文件跟项目走
# crontab -e
10 * * * * /usr/sbin/logrotate -f /app/tomcat_logrotate