Logrotate配置

Logrotate配置

😄 Written by Zak Zhu

参考

Logrotate Description

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large.

Logrotate Command

# Usage:
logrotate [OPTION...] <configfile>
# Options:
	-d, --debug			# Don't do anything, just test and print debug messages
	-f, --force			# Force file rotation
	-v, --verbose		# Display messages during rotation

Logrotate Configuration

Config Files

  • /etc/logrotate.conf
  • /etc/logrotate.d/

Config Options

参数 注释
compress 通过gzip压缩转储后的日志
delaycompress 和compress一起使用时, 转储的日志文件到下一次转储时才压缩
copytruncate 用于还在打开中的日志文件, 把当前日志备份并截断
(是先拷贝再清空的方式, 拷贝和清空之间有一个时间差, 可能会丢失部分日志数据)
create 0644 tomcat tomcat 轮转时, 指定新文件的属性
missingok 如果日志丢失, 不报错继续滚动下一个日志
notifempty 当日志文件为空时, 不进行轮转
olddir DIR 转储后的日志文件放入指定的目录
(必须和当前日志文件在同一个文件系统)
sharedscripts
prerotate | postrotate
endscript
daily
weekly
monthly
rotate 7 保留7个转储日志
dateext
size 100M 当日志达到指定大小时才转储, 单位: byte, k, M, G

Config Examples

  • Just a little test

    vim /etc/logrotate.d/test

    /tmp/test.log {

    ​ su root root
    ​ daily
    ​ rotate 1
    ​ size 10k
    ​ dateext
    ​ notifempty
    ​ compress

    }

    1

  • Nginx log rotate

    vim /etc/logrotate.d/nginx

    /home/zak/nginx/logs/*.log {

    ​ daily

    ​ rotate 365

    ​ dateext

    ​ copytruncate

    ​ compress

    ​ delaycompress

    ​ missingok

    ​ notifempty

    ​ sharedscripts

    ​ postrotate

    ​ /etc/init.d/nginx reload

    ​ endscript

    }

posted @ 2019-10-07 23:31  ZakZhu  阅读(374)  评论(0编辑  收藏  举报