使用logrotate切割日志

简介

通常在服务器上都有这样的烦恼,就是日志文件会随着时间的推移变得越来越大。需要人工的手动清理,有些程序自己并不支持日志的分割,会导致单个 log 文件庞大,影响效率。

logrotate是为了方便管理生成大量日志文件的系统而设计的。 它允许自动分割、压缩。移除和邮寄日志文件。 每个日志文件可以每天、每周、每月或当日志文件过大时处理。

通常情况下,logrotate是作为一个每天的cron作业来运行的。 它不会在一天内多次修改一个日志,除非该日志的标准是基于日志的大小,并且每天都要运行多次,或者除非使用了 -f 或 --force 选项。

logrotate 的命令是

logrotate [-dv] [-f|--force] [-s|--state file] config_file ..
   -?, --help
          Prints help message.

   -d, --debug
          Turns on debug mode and implies -v.  In debug mode, no changes will be made to the logs or to the logrotate state file.

   -f, --force
          Tells logrotate to force the rotation, even if it doesn't think this is necessary.  Sometimes this is useful after adding new entries to
          a logrotate config file, or if old log files have been removed by hand, as the new files will be created, and logging will continue cor‐
          rectly.

   -m, --mail <command>
          Tells  logrotate which command to use when mailing logs. This command should accept two arguments: 1) the subject of the message, and 2)
          the recipient. The command must then read a message on standard input and mail  it  to  the  recipient.  The  default  mail  command  is
          /bin/mail -s.

   -s, --state <statefile>
          Tells  logrotate  to  use an alternate state file.  This is useful if logrotate is being run as a different user for various sets of log
          files.  The default state file is /var/lib/logrotate.status.

   --usage
          Prints a short usage message.

   +-v, --verbose
          Turns on verbose mode.

一般测试配置文件是否正确就会使用 logrotate -df config_gile 这样就是在 debug 模式下立马模拟一次日志切分,可以在控制台上看到 logrotate 的执行结果,从而判断配置文件编写的是否正确。

[root@chenglong ~]# logrotate -df /etc/logrotate.d/nginx
reading config file /etc/logrotate.d/nginx

Handling 1 logs

rotating pattern: /usr/local/nginx/logs/access.log   forced from command line (7 rotations)
empty log files are not rotated, old logs are removed
considering log /usr/local/nginx/logs/access.log
  log needs rotating
rotating log /usr/local/nginx/logs/access.log, log->rotateCount is 7
dateext suffix '-20200528'
glob pattern '-[0-9][0-9][0-9][0-9][0-9][0-9][0-9][0-9]'
glob finding logs to compress failed
glob finding old rotated logs failed
renaming /usr/local/nginx/logs/access.log to /usr/local/nginx/logs/access.log-20200528
running postrotate script
running script with arg /usr/local/nginx/logs/access.log  : "
    [ -e /usr/local/nginx/logs/nginx.pid ] && kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
"

logrotate 默认的配置文件在 /etc/logrotate.conf,该配置文件 include /etc/logrotate.d 文件夹,所以我们自己编写的配置文件,就放在该文件夹下面就好,比如我下面要分享的 nginx 日志切分,就是创建在 /etc/logrotate.d 文件夹下,名字就叫做 nginx

快速案例

切分 nginx 的 access.log 日志

每天执行,历史日志保存 7 天,昨天的日志不做压缩,前天以前的日志进行 gz 压缩。

/usr/local/nginx/logs/access.log  {
daily
rotate 7
missingok
dateext
compress
delaycompress
notifempty
sharedscripts
olddir historyLogDir
postrotate
    [ -e /usr/local/nginx/logs/nginx.pid ] && kill -USR1 `cat /usr/local/nginx/logs/nginx.pid`
endscript
}

配置参数

compress 使用gzip来压缩日志文件

nocompress 日志不压缩的参数

compresscmd 指定压缩工具,默认gzip

uncompresscmd 指定解压工具,默认gunzip

delaycompress 推迟要压缩的文件,直到下一轮询周期再执行压缩,可与compress搭配使用

dateext 轮询的文件名字带有日期信息

dateformat 格式化归档日期后缀,只有%Y, %m, %d 和`%s

daily 日志按天轮询

weekly 日志按周轮询

monthly 日志按月轮询

yearly 日志按年轮询日志

maxage count 删除count天前的轮询日志文件

rotate count 删除count个外的轮询日志文件

notifempty 文件为空时,不进行轮询

missingok 如果没有日志文件也不报错

size size 日志文件根据大小规定进行轮询,默认单位是byte,也可指定kb, M, G

minsize size 文件大小超过size后才能进行轮询,此时会略过时间参数

postrotate/endscript 在所有其它指令完成后,postrotate和endscript里面指定的命令将被执行。

sharedscripts 在所有的日志文件都轮询完毕后统一执行一次脚本。当声明日志文件的时候使用通配符时需要用到

olddir 指定切割后的日志文件存放的文件夹

create mode owner group 新日志文件的权限

猜你喜欢

转载自www.cnblogs.com/manastudent/p/12983548.html
今日推荐