我们有一个Java应用程序,它使用log4j并每天轮换自己的日志:
/var/log/foo/foo.log /var/log/foo/foo.log.YYYY-MM-dd 我们宁可不改变它的configuration…
但是,我们确实希望:
虽然写一个cron-job并不难,但我们宁愿停留在logrotate的框架中。
我创build了以下/etc/logrotate.d/foo :
/var/log/foo/foo.log.* { daily rotate 2 compress delaycompress missingok notifempty }
但它没有做任何事情:
reading config file /etc/logrotate.d/foo Handling 1 logs rotating pattern: /var/log/foo/foo.log.* after 1 days (2 rotations) empty log files are not rotated, old logs are removed considering log /var/log/foo/foo.log.2017-06-28 log does not need rotating considering log /var/log/foo/foo.log.2017-06-29 log does not need rotating considering log /var/log/foo/foo.log.2017-06-30 log does not need rotating considering log /var/log/foo/foo.log.2017-07-01 log does not need rotating considering log /var/log/foo/foo.log.2017-07-02 log does not need rotating considering log /var/log/foo/foo.log.2017-07-03 log does not need rotating considering log /var/log/foo/foo.log.2017-07-04 log does not need rotating
当它们的总数超过2时,如何压缩文件并删除最老的文件?
我真的不认为你会用没有外部命令的logrotate来做你想做的事情。 另外,模式/var/log/foo/foo.log.*会将每个单独的文件看作是一个单独的日志,而不是一组旋转的文件。
man logrotate请谨慎使用通配符。 如果指定*,logrotate将旋转所有文件,包括以前旋转的文件。
无论如何,也许这样的事情? 只需设置你永远不会达到的旋转条件,因为log4j已经部分处理了旋转。 然后把其余的在一个prerotate脚本?
/var/log/foo/foo.log { size 100g # something big that will never be reached/rotated since log4j does this. missingok notifempty # delete the other logs over 7 days old prerotate find /var/log/foo/ -name 'foo.log.*' -mtime +7 -delete nice gzip /var/log/foo/foo.log.* endscript }