日志切割的方法

2022-12-31 1531阅读

温馨提示:这篇文章已超过482天没有更新,请注意相关的内容是否还可用!

之前不懂日志切割,一直觉得很牛逼。 其实这个东西很简单。 如果你仔细阅读这篇文章crontab查看日志,你几乎可以在一天内理解它。 这是一件非常简单的事情。 不要被名字所迷惑。 害怕的!

日志切割的方法
(图片来源网络,侵删)
日志切割的方法
(图片来源网络,侵删)

1. linux自带的logrotate日志切割

2、logrotate配置文件分为主配置和子配置

主要配置:/etc/logrotate.conf

子配置:/etc/logrotate.d/下的文件

3、logrotate是基于CRON的crontab查看日志,它的脚本是/etc/cron.daily/logrotate

[root@localhost ~]# cat /etc/cron.daily/logrotate 
#!/bin/sh
/usr/sbin/logrotate -s /var/lib/logrotate/logrotate.status /etc/logrotate.conf
EXITVALUE=$?
if [ $EXITVALUE != 0 ]; then
 /usr/bin/logger -t logrotate "ALERT exited abnormally with [$EXITVALUE]"
fi
exit 0

实际运行时会调用配置文件/etc/logrotate.conf

4.logrotate基于CRON运行,所以执行时间由CRON控制

在ubuntu上查看/etc/crontab

centos查看/etc/anacrontab

root@qqq:/etc/logrotate.d# cat /etc/crontab 
# /etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user command
17 * * * * root cd / && run-parts --report /etc/cron.hourly
25 6 * * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6 * * 7 root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6 1 * * root test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )
#

没错,run-parts就是运行一个目录下的所有脚本或者程序。 --report 的作用和--verbose 类似,自己理解即可

没错,logrotate脚本就是/etc/cron.daily/目录下的脚本,CRON每天06:25运行/etc/cron.daily/目录下的所有脚本

五、logrotate命令:

logrotate命令格式:
logrotate [OPTION...] 
-d, --debug :debug模式,测试配置文件是否有错误。
-f, --force :强制转储文件。
-m, --mail=command :压缩日志后,发送日志到指定邮箱。
-s, --state=statefile :使用指定的状态文件。
-v, --verbose :显示转储过程。

6、logrotate配置文件常用参数

compress 通过gzip 压缩转储以后的日志
nocompress 不做gzip压缩处理
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate 备份日志文件不过不截断
create mode owner group 轮转时指定创建新文件的属性,如create 0777 nobody nobody
nocreate 不建立新的日志文件
delaycompress 和compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩。
missingok 如果日志丢失,不报错继续滚动下一个日志
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使日志文件为空文件也做轮转,这个是logrotate的缺省选项。
notifempty 当日志文件为空时,不进行轮转
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
sharedscripts 运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
prerotate 在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;必须独立成行
postrotate 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行
daily 指定转储周期为每天

weekly 指定转储周期为每周 monthly 指定转储周期为每月 rotate count 指定日志文件删除之前转储的次数,0 指没有备份,5 指保留5 个备份 dateext 使用当期日期作为命名格式 dateformat .%s 配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数 size(或minsize) log-size 当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem). 当日志文件 >= log-size 的时候就转储。 以下为合法格式:(其他格式的单位大小写没有试过) size = 5 或 size 5 (>= 5 个字节就转储) size = 100k 或 size 100k size = 100M 或 size 100M

七、附上生产环境的几个配置文件

root@web1:/etc/logrotate.d# cat nginx
/var/log/nginx/*.log {
 daily
 missingok
 rotate 14
 compress
 delaycompress
 notifempty
 create 0640 www-data adm
 sharedscripts
 prerotate
 if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
 run-parts /etc/logrotate.d/httpd-prerotate; \
 fi \
 endscript
 postrotate
 invoke-rc.d nginx rotate >/dev/null 2>&1
 endscript
}

zabbix这个

root@web1:/etc/logrotate.d# cat zabbix-agent 
/var/log/zabbix/zabbix_agentd.log {
 weekly
 rotate 12
 compress
 delaycompress
 missingok
 notifempty
 create 0640 zabbix zabbix
}

php这个慢查询和错误日志

root@web1:/etc/logrotate.d# cat php7.0-fpm 
/var/log/php7.0-fpm.log {
 rotate 12
 weekly
 missingok
 notifempty
 compress
 delaycompress
 postrotate
 /usr/lib/php/php7.0-fpm-reopenlogs
 endscript
}
/var/log/php/*.log {
 rotate 12
 daily
 missingok
 notifempty
 compress
 delaycompress 
}

日志切割的方法

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]