logrotate 一个经常写入的日志而不丢失数据

logrotate 一个经常写入的日志而不丢失数据

我想通过捕获缓慢的查询logrotate,并且我希望它们每周轮换一次,并且我想节省一年的价值。日志采用以下形式:

-rw-r-----. 1 mysql  root           1239 Feb 21 18:46 mysqld1-slow.log
-rw-r-----. 1 mysql  root            885 Feb 11 14:48 mysqld2-slow.log
-rw-r-----. 1 mysql  root            885 Feb 22 08:58 mysqld3-slow.log
-rw-rw-rw-. 1 mysql  root            802 Feb 11 14:47 mysqld-slow.log

由于日志最终被频繁写入,我如何确保没有遗漏任何内容logrotate?该进程本身不会创建文件,它需要有原始文件。我想这会做到这一点:

/var/log/mysqld*-slow.log {
    missingok
    notifempty
    weekly
    rotate 52
    compress
    delaycompress
    create 0644 mysql root
}

因此它应该压缩旧的,并使用正确的权限创建相同的文件名,但我不确定 logrotate 如何处理在运动中写入的内容。

答案1

该函数似乎copytruncate可以完成此任务:

copytruncate Truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one. It can be used when some program cannot be told to close its logfile and thus might continue writing (appending) to the previous log file forever. Note that there is a very small time slice between copying the file and truncating it, so some logging data might be lost. When this option is used, the create option will have no effect, as the old log file stays in place.

我已经把这个落实到位了。

相关内容