如何使用php日志配置logrotate

时间:2013-01-03 19:30:39

标签: php linux logrotate

我正在运行带有APC的php5 FPM作为操作码和应用程​​序缓存。像往常一样,我将php错误记录到文件中。

由于这变得非常大,我尝试配置logrotate。它可以工作,但是在轮换之后,php会继续记录到现有的日志文件,即使重命名它也是如此。这导致scripts.log为0B文件,scripts.log.1继续增长。

我认为(还没试过)在postrotate中运行php5-fpm reload可以解决这个问题,但每次都会清除我的APC缓存。

有人知道如何正常工作吗?

2 个答案:

答案 0 :(得分:6)

  

我发现logrotate的“copytruncate”选项可确保inode不会更改。基本上什么是[原文如此!]正在寻找。

这可能就是你要找的东西。取自:How does logrotate work? - Linuxquestions.org

正如我在评论中所写,您需要阻止PHP写入相同(重命名)的文件。复制文件通常会创建一个新文件,并且截断也是该选项名称的一部分,所以我认为, copytruncate 选项是一个简单的解决方案(from the manpage):< / p>

  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 can not 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  log-
          ging  data  might be lost.  When this option is used, the create
          option will have no effect, as the old log file stays in  place.

另见:

答案 1 :(得分:5)

我在服务器上找到的另一个解决方案是告诉php重新打开日志。我认为nginx也有这个功能,这让我觉得它一定是很常见的地方。这是我的配置:

/var/log/php5-fpm.log {
        rotate 12
        weekly
        missingok
        notifempty
        compress
        delaycompress
        postrotate
                invoke-rc.d php5-fpm reopen-logs > /dev/null
        endscript
}