概要
在日常运维中,日志切割是一个比较基础且重要的操作,可以自己编写shell实现,也可以借助于系统自带的软件来实现,今天我们使用linux自带的logrotate来实现对nginx日志的切割。
技术细节
Linux系统默认安装logrotate工具,它默认的配置文件在:
/etc/logrotate.conf
/etc/logrotate.d/
logrotate.conf 才主要的配置文件,logrotate.d 是一个目录,该目录里的所有文件都会被主动的读入/etc/logrotate.conf中执行,类似于nginx的vhost。
另外,如果 /etc/logrotate.d/ 里面的文件中没有设定一些细节,则会以/etc/logrotate.conf这个文件的设定来作为默认值。
查看版本
logrotate --version
# 查看默认配置文件
cat /etc/logrotate.conf
[root@ziyi ~]# cat /etc/logrotate.conf
weekly # 默认每周执行一次日志轮询
rotate 4 # 默认保留4个日志文件
create # 自动创建新的日志文件,新的文件和原来的文件具有相同的权限
dateext # 日志切割后,文件以当前日志为结尾,例如:messages-20181125
#compress # 指定不压缩转储文件,如果需要压缩,去掉注释就可以了。
include /etc/logrotate.d # 将/etc/logrotate.d目录中的配置文件加载进来
/var/log/wtmp {
# 针对wtmp日志的配置参数
monthly # 每月切割一次
create 0664 root utmp # 新建日志的权限为0644,用户为root,组为utmp
minsize 1M # 文件大小超过1M后才会切割
rotate 1