何为Logrotate?php
Logrotate是一个实用的日志管理工具,旨在简化对系统上生成大量的日志文件进行管理。 Logrotate容许自动旋转压缩,删除和邮寄日志文件,从而节省宝贵的磁盘空间。 Logrotate能够设置为天天、每周、每个月或当日志文件达到必定的大小时处理日志文件。还能够彻底控制日志的自动化管理方式,而不须要人工干预。Logrotate支持Linux系统上的全部日志文件,包括但不限于Apache
、Nginx
、Tomcat
、ELK
、zabbix
等应用。node
1.安装logrotatepython
系统默认已经安装,某些Linux发行版可能并未安装,那么请执行如下命令安装:nginx
Centos:apache
$ yum install logrotate -y
Ubuntu:vim
$ apt-get install logrotate -y
经过yum默认安装的文件存储位置说明tomcat
/etc/cron.daily # logrotate脚本文件存放位置 /etc/logrotate.conf # 主配置文件 /usr/sbin/logrotate # 二进制程序文件 /etc/logrotate.d/ # 特定服务日志存储目录(本身设定的配置文件) /var/lib/logrotate.status # 状态文件
2.查看logrotate主文件默认配置状况bash
$ cat /etc/logrotate.conf # see "man logrotate" for details # rotate log files weekly weekly # 每周转存一次 # keep 4 weeks worth of backlogs rotate 4 # 保留四个日志备份文件 # create new (empty) log files after rotating old ones create # rotate后,建立一个新的空文件 # use date as a suffix of the rotated file dateext # 轮转的文件名字带有日期信息 # uncomment this if you want your log files compressed #compress # RPM packages drop log rotation information into this directory include /etc/logrotate.d # 此目录下的配置文件优先生效 # no packages own wtmp and btmp -- we'll rotate them here /var/log/wtmp { # 指定/var/log/wtmp日志文件; monthly # 每个月轮转一次,优先于全局设定的每周轮转一次; minsize 1M # 日志文件大于1M才会去轮转; create 0664 root utmp # 新日志文件的权限,属主,属组; rotate 1 # 保留一个日志备份,优先于全局设置的四个; } /var/log/btmp { # 指定/var/log/btmp日志文件; missingok # 若是日志丢失,不报错; monthly create 0600 root utmp rotate 1 } # system-specific logs may be also be configured here. # 系统特定的日志也能够在主文件中配置。
3.logrotate日志切割dom
在定义日志文件时可使用通配符,但不建议使用,由于它会包括已切换过的日志,如必须使用,请在*
号后加上已知的文件扩展名, 例如:*.log
。ide
Nginx日志:
在/etc/logrotate.d/
目录下建立Nginx服务日志配置文件
$ vim /etc/logrotate.d/nginx /usr/local/nginx/logs/*.log { create 644 www root daily dateext rotate 3 minsize 10K copytruncate nocompress missingok notifempty noolddir postrotate /bin/kill -USR1 `cat /usr/local/nginx/logs/nginx.pid` endscript }
php日志:
/usr/local/php/var/log/*.log { missingok notifempty sharedscripts delaycompress create 0664 www www postrotate /bin/kill -SIGUSR1 `cat /usr/local/php/var/run/php-fpm.pid 2>/dev/null` 2>/dev/null || true endscript }
注:你也能够手动生成一个20M内容的日志文件进行测试,例如:
$ head -c 20M < /dev/urandom > /var/log/renwole-log
配置完成后,能够经过以下命令来手动执行查看效果:
$ logrotate -f /etc/logrotate.d/nginx
另外还可使用以下命令查看logrotate运行状态:
$ cat /var/lib/logrotate/logrotate.status
因为Logrotate是基于cron定时运行的,因此logrotate脚本默认在 /etc/cron.daily/
目录下,文件名是logrotate。你能够设置 /etc/anacrontab
来控制Logrotate什么时候运行。
4.查看anacrontab默认配置
$ cat /etc/anacrontab # /etc/anacrontab: configuration file for anacron # See anacron(8) and anacrontab(5) for details. SHELL=/bin/sh PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # the maximal random delay added to the base delay of the jobs RANDOM_DELAY=45 # the jobs will be started during the following hours only START_HOURS_RANGE=3-22 #period in days delay in minutes job-identifier command 1 5 cron.daily nice run-parts /etc/cron.daily 7 25 cron.weekly nice run-parts /etc/cron.weekly @monthly 45 cron.monthly nice run-parts /etc/cron.monthly
5.关于logrotate经常使用配置参数注解
daily,weekly,monthly # 转储周期分别是天天/每周/每个月; minsize 15M # 日志体积大于此值时轮换(例如:100K,4M); dateext # 轮询的文件名字带有日期信息; missingok # 若是日志文件丢失,不要显示错误; rotate 5 # 轮转存储中包含多少备份日志文件,0为无备份,以数字为准; compress # 经过gzip压缩转储之后的日志,以*.gz结尾; nocompress # 不须要压缩时,用这个参数; delaycompress # 延迟压缩,和compress一块儿使用时压缩全部日志,除当前和下一个最近的; nodelaycompress # 覆盖delaycompress选项,转储同时压缩; copytruncate # 用于还在打开中的日志文件,把当前日志备份并截断; nocopytruncate # 备份日志文件可是不截断; create 644 www root # 转储文件,使用指定的文件模式建立新的日志文件; nocreate # 不创建新的日志文件; errors renwole@my.org # 专储时的错误信息发送到指定的Email地址; ifempty # 即便是空文件也转储,这个是logrotate的缺省选项; notifempty # 若是日志文件为空,则不转储; mail renwole@my.org # 把转储的日志文件发送到指定的E-mail地; nomail # 转储时不发送日志文件; olddir /tmp # 转储后的日志文件放入指定目录,必须和当前日志文件在同一个文件系统; noolddir # 转储后的日志文件和当前日志文件放在同一个目录下; prerotate/endscript # 在转储之前须要执行的命令能够放入这个对,这两个关键字必须单独成行; postrotate/endscript # 在转储之后须要执行的命令能够放入这个对,这两个关键字必须单独成行; tabooext # 不转储指定扩展名的文件,缺省扩展名:cfsaved,.disabled,.dpkg-dist等; sharedscripts # 共享脚本,让postrotate/endscript包含脚本只执行一次便可; dateformat # 配合dateext使用能够为切割后的日志加上YYYYMMDD格式的日期;
以上参数均可以在全局主配置文件中定义,或指定为某个日志文件进行配置,注意:使用时参数之间不要冲突。
python uwsgi 日志分割
[root@test-server28 logrotate.d]# cat uwsgi
/data/logs/uwsgi/*/access.log {
# 天天分割
daily
# 保存30天
rotate 30
# 日期后缀
dateext
# 日志丢失不报错
missingok
# 空文件不转储
notifempty
# 压缩
#compress
# 共享脚本
sharedscripts
postrotate
touch /data/logs/uwsgi/touchforlogrotate
endscript
}
切割日志时出现报错
error: skipping "/home/deploy/tomcat/apache-tomcat-7.0.75-df/logs/catalina.out" because parent directory has insecure permissions (It's world writable or writable by group which is not "root") Set "su" directive in config file to tell logrotate which user/group should be used for rotation.
xx 文件所属用户 添加“su deploy deploy ”到/etc/logrotate.d/tomcat文件中便可 以下; /home/deploy/tomcat*********/logs/*.out { su deploy deploy copytruncate daily rotate 30 dateext missingok notifempty }
[root@**** logrotate.d]# logrotate -f /etc/logrotate.d/tomcat [root@**** logs]# ls catalina.out catalina.out-20181210
对分割日志进行定时的删除 #!/bin/bash /bin/find /mnt/log/tomcat/ -name "*.out*" -mtime +15 -type f|xargs rm -rf /bin/find /home/deploy/*******/logs/ -name "*.out*" -mtime +1 -type f -print0 |xargs -0 mv -t /mnt/log/tomcat/