【问题标题】:How to configure logrotate with php logs如何使用 php 日志配置 logrotate
【发布时间】:2012-12-18 05:20:49
【问题描述】:

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

由于它变得相当大,我尝试配置 logrotate。它可以工作,但是在轮换之后,php 继续记录到现有的日志文件,即使它被重命名。这导致 scripts.log 成为 0B 文件,并且 scripts.log.1 继续进一步增长。

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

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

【问题讨论】:

  • 当您轮换日志时,我认为您也需要重新启动 FPM。或者您需要告诉 logrotate 实际创建一个新的 inode 并截断现有文件。否则,FPM 将继续写入日志文件 inode,因此单独重命名文件无济于事(这就是您在问题中描述的行为)。
  • copytruncate 只是你的救命稻草..

标签: php linux logrotate


【解决方案1】:

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

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

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

  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.

另见:

【讨论】:

  • 完美!像魅力一样工作
【解决方案2】:

我在我的服务器上找到的另一个解决方案是告诉 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
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2016-09-08
    • 1970-01-01
    • 2020-11-11
    • 2010-11-16
    • 2019-07-03
    • 2018-06-12
    • 2017-04-30
    • 1970-01-01
    相关资源
    最近更新 更多