Ubuntu

如何在 logrotate 配置文件中指定萬用字元設置的例外?

  • February 25, 2018

對於我的日誌文件,我遇到的問題是除了一個(或幾個)文件之外的所有文件都使用相同的配置,而其余文件則有另一個。我試圖通過為所有文件提供通用配置來實現這一點,然後稍後為少數特定文件覆蓋此配置,例如:

/var/log/mylogs/*.log {
                  size 1000k
                  copytruncate
                  create 0644 root root
                  rotate 99
                  compress
                  missingok
}

/var/log/mylogs/thatonespecial.log {
                  size 1000k
                  copytruncate
                  create 0644 myuser mygroup
                  rotate 99
                  compress
                  missingok
}

但是,這會引發錯誤:

error: /var/log/mylogs/logrotate.conf:10 duplicate log entry for /var/log/mylogs/thatonespecial.log

我應該如何正確處理這種情況?我當然不想單獨列出大量的標準日誌文件,所以使用萬用字元配置對我來說似乎是合理的。但是我怎樣才能在萬用字元文件中指定一個例外呢?

最優雅的答案是放在thatonespecial.log一個單獨的目錄中,這樣它就無法匹配萬用字元。

如果這不起作用,那麼您可以使用glob來縮小萬用字元的範圍。這很亂,但如果你絕對不能移動文件位置,那麼它可能是你唯一真正的選擇。像這樣的東西:

/var/log/mylogs/[!t][!h]*.log

將匹配名稱中至少包含 2 個字元且不以“th”開頭的任何 .log 文件。

似乎已經實現了規則的覆蓋並且它現在可以工作了:

$ logrotate --version
logrotate 3.8.7

$ cat /etc/logrotate.d/test
# rotate application logs for 40 days by default
/home/myapp/log/*.log
/home/myapp/log/*/*.log
{
   daily
   compress
   delaycompress
   rotate 40
}

# rotate access logs for 1 year
/home/myapp/log/access/*.log {
   daily
   compress
   delaycompress
   rotate 365
}

$ logrotate  -d /etc/logrotate.d/test
reading config file /etc/logrotate.d/test

Handling 2 logs

rotating pattern: /home/myapp/log/*.log
/home/myapp/log/*/*.log
after 1 days (40 rotations)
empty log files are not rotated, old logs are removed
No logs found. Rotation not needed.

rotating pattern: /home/myapp/log/access/*.log  after 1 days (365 rotations)
empty log files are not rotated, old logs are removed
No logs found. Rotation not needed.

在我的本地 logrotate(3.7.8 版)上進行測試時,出現了錯誤:

$ cat logr.conf 
# rotate application logs for 40 days by default
/home/myapp/log/*.log
/home/myapp/log/*/*.log
{
   daily
   compress
   delaycompress
   rotate 40
}

# rotate access logs for 1 year
/home/myapp/log/access/*.log {
   daily
   compress
   delaycompress
   rotate 365
}

$ logrotate -d logr.conf 
reading config file logr.conf
reading config info for /home/myapp/log/*.log
/home/myapp/log/*/*.log

error: logr.conf:12 duplicate log entry for /home/myapp/log/access/api_access.log
error: found error in /home/myapp/log/access/*.log , skipping
removing last 1 log configs

...

引用自:https://serverfault.com/questions/665273