运维时,你是否有单个日志文件太大打不开而发愁,担心日志占用磁盘空间满了。下面就介绍下linux下自带的logrotate。
开发java时,多数用的log4j2,就能满足上述需要。但是像tomcat的catalina.out的日志不支持滚动,像nginx的host.access.log等也是如此。
本人实践的操作系统为Ubuntu20.04。
参考资料为:
logrotate一般为linux系统自带。配置文件的位置在/etc/logrotate.conf。
#see "man logrotate" for details #rotate log files weekly
weekly
#use the adm group by default, since this is the owning group #of /var/log/syslog.
su root adm
#keep 4 weeks worth of backlogs
rotate 4
#create new (empty) log files after rotating old ones
create
#use date as a suffix of the rotated file
#dateext
#uncomment this if you want your log files compressed
#压缩
#compress
#packages drop log rotation information into this directory
#包含子配置
include /etc/logrotate.d
#system-specific logs may be also be configured here.
子配置文件放置到/etc/logrotate.d的目录下即可。如:nginx_goproxy
/data/nginx/logs/*.log
/data/goproxy/goproxy.log
{
#用户用户组,解决权限问题
su root root
#没有上面配置的日志不报错
missingok
#没有日志内容不切割
notifempty
#每天切割
daily
#保留文件的个数
rotate 366
#复制日志文件并将旧日志文件内容清空。create是将旧文件重命名,创建新的的文件。
#由于tomcat7不支持不重启重新打开日志顾采用copytruncate,nginx支持reopen可以结合
#postrotate/endscript采用create。
copytruncate
#超过文件大小就分割
size 200M
#文件后缀加日期 dateext
}