为什么用logrotate
因为它丝滑,用脚本难免会丢失一部分日志.logrotate 在github上显示创建于1996年
创建/etc/logrotate.d/ac_all
/data/module/core/assist/execute.log /data/module/core/course/execute.log /data/module/core/logger/execute.log /data/module/core/order/execute.log /data/module/core/stu/execute.log /data/module/core/system/execute.log /data/module/core/tch/execute.log /data/module/core/user/execute.log /data/module/core/user-relation/execute.log /data/module/core/video/execute.log /data/module/ac-bi/execute.log /data/module/ac-client/execute.log /data/module/ac-cms/execute.log /data/module/ac-common/execute.log /data/module/ac-crm/execute.log /data/module/ac-dispatch/execute.log /data/module/ac-dispatch2/execute.log /data/module/ac-dist/execute.log /data/module/ac-eureka/execute.log /data/module/ac-mkt/execute.log /data/module/ac-mq/execute.log /data/module/ac-order/execute.log /data/module/ac-timer/execute.log /data/module/ac-websocket/execute.log /data/module/ac-wehub/execute.log /data/module/ac-write/execute.log /data/module/ac-zuul/execute.log { dateext dateformat -%Y%m%d%s rotate 5 size=50M notifempty missingok create 0664 ac-dev ac-dev copytruncate } |
debug测试 -d, --debug Don't do anything, just test (implies -v)
logrotate -d ac_all
执行并输出信息
logrotate -v ac_all
执行后的日志格式及文件大小。
注意这里写日志的脚本 需要改成>> 追加形式,如果是>的话 会导致新的日志文件和原来的一样大小
>方式切割的日志
>>方式切割的日志
建议不要开启压缩,因为压缩会比较占用cpu,或者压缩的时间在cpu空闲期进行。
logrotate配置参数
logrotate 全局配置文件: /etc/logrotate.conf
配置参数 功能说明
compress 通过gzip 压缩转储以后的日志
nocompress 不需要压缩时,用这个参数
copytruncate 用于还在打开中的日志文件,把当前日志备份并截断;是先拷贝再清空的方式,拷贝和清空之间有一个时间差,可能会丢失部分日志数据。
nocopytruncate 备份日志文件但是不截断
create mode owner group 转储文件,使用指定的文件模式创建新的日志文件。轮转时指定创建新文件的属性,如create 0777 nobody nobody
nocreate 不建立新的日志文件
delaycompress 和 compress 一起使用时,转储的日志文件到下一次转储时才压缩
nodelaycompress 覆盖 delaycompress 选项,转储同时压缩
errors address 专储时的错误信息发送到指定的Email 地址
ifempty 即使是空文件也转储,这个是 logrotate 的缺省选项。
notifempty 如果是空文件的话,不转储
mail address 把转储的日志文件发送到指定的E-mail 地址
nomail 转储时不发送日志文件
olddir directory 转储后的日志文件放入指定的目录,必须和当前日志文件在同一个文件系统
noolddir 转储后的日志文件和当前日志文件放在同一个目录下
prerotate/endscript
在logrotate转储之前需要执行的指令,例如修改文件的属性等动作;这两个关键字必须单独成行;
postrotate/endscript 在logrotate转储之后需要执行的指令,例如重新启动 (kill -HUP) 某个服务!必须独立成行;
daily 指定转储周期为每天
weekly 指定转储周期为每周
monthly 指定转储周期为每月
rotate count 指定日志文件删除之前转储的个数,0 指没有备份,5 指保留5个备份
tabootext [+] list 让logrotate 不转储指定扩展名的文件,缺省的扩展名是:.rpm-orig, .rpmsave, v, 和 ~
size size当日志文件到达指定的大小时才转储,Size 可以指定 bytes (缺省)以及KB (sizek)或者MB (sizem).
missingok 如果日志丢失,不报错继续滚动下一个日志
notifempty 当日志文件为空时,不进行轮转
sharedscripts 运行postrotate脚本,作用是在所有日志都轮转后统一执行一次脚本。如果没有配置这个,那么每个日志轮转后都会执行一次脚本
dateext 使用当期日期作为命名格式
size(或minsize) log-size 当日志文件到达指定的大小时才转储,log-size能指定bytes(缺省)及KB (sizek)或MB(sizem).
dateformat .%s 配合dateext使用,紧跟在下一行出现,定义文件切割后的文件名,必须配合dateext使用,只支持 %Y %m %d %s 这四个参数 自己测试了%H也可以。不过如果需要支持分的话logrotate需要升级到3.9版本 ,
Support for %H was added in version 3.9.0. In earlier versions, logrotate did not support strftime "%H:
dateformat format_string: Specify the extension for dateext using the notation similar to strftime(3) function. Only %Y %m %d and %s specifiers are allowed.
From the logrotate man page http://linux.die.net/man/8/logrotate
However, you can use %s in the dateformat string, which is the number of seconds since 1970-01-01. You can set dateformat -%Y%m%d-%s. This will produce unique filenames every time the log is rotated, so you can rotate the file multiple times a day. Unfortunately, the %s part will not be easy to read, but you can easily convert it back into a readable date with perl -e "print scalar(localtime(1451214849))".
On some systems, the date program allows to do such conversion easily with date -d @1451214849 (e.g. GNU date). On most systems (including e.g. Solaris date), you may have luck with syntax like date -d "1970-01-01 + 1451214849 sec". Note that Busybox date supports only the @ trick but not complex expressions of the second example.
使用定时任务调用logrotate 命令
为什么要用定时任务来调用,因为logrotate的最小执行时间为每天执行一次,但是日志不会等到一天才需要切分,里面的size 的定义指的是
只有在满足执行机制的前提下,而且日志大小满足切分要求,才会进行切分。