在多个地方定义的规则的 Logrotate 行为

Logrotate behaviour for rules defined in multiple places

我需要你的帮助来理解 logrotate 行为。

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

# 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 {
    monthly
    create 0664 root utmp
    minsize 1M
    rotate 1
}

/var/log/btmp {
    missingok
    monthly
    create 0600 root utmp
    rotate 1
}

# system-specific logs may be also be configured here.

logrotate.d 目录中,我有一个名为 consul-log 的文件:

/var/log/consul {
    size 500M
    missingok
    rotate 0
    compress
    notifempty
    copytruncate
}    

/var/lib/logrotate.status 文件中,我可以看到这些行(与 consul 日志文件相关):

logrotate state -- version 2
"/var/log/consul" 2016-1-25

我的问题是:

  1. 如果我在logrotate.conf里面有rotate 4,但是我在logrotate.d/consul-log里面有rotate 0,logrotate会使用rotate 0还是rotate 4

关于你的问题:

If I have rotate 4 inside logrotate.conf, but I have rotate 0 inside logrotate.d/consul-log, will logrotate use rotate 0 or rotate 4?

logrotate.conf 是系统范围更改的配置文件,您在 logrotate.d 文件夹中创建的任何其他配置文件都将被覆盖。

因为这条指令:

# RPM packages drop log rotation information into this directory
include /etc/logrotate.d

对于/var/log/consul,它将使用rotate 0。引自 man page:

Each configuration file can set global options (local definitions override global ones, and later definitions override earlier ones) and specify logfiles to rotate.

在你的情况下,rotate 0 既是本地的又是后来的。