如何使用PHP日志configurationlogrotate

我使用APC作为操作码和应用程​​序caching运行php5 FPM。 像往常一样,我将PHP错误logging到文件中。

由于变得相当大,我试图configurationlogrotate。 它的工作原理,但循环后,PHP继续login到现有的日志文件,即使它被重命名。 这导致scripts.log是0B文件,而scripts.log.1继续增长。

我认为(没有试过),在postrotate运行php5-fpm重新加载可以解决这个问题,但是这将清除我的APCcaching每一次。

有人知道如何正确地工作吗?

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

这可能是你在找什么。 采取: logrotate如何工作? – Linuxquestions.org 。

正如我的评论中写的,你需要防止PHP写入相同(重命名)的文件。 复制文件通常会创建一个新的文件,截断也是选项名称的一部分,所以我认为, 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 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. 

也可以看看:

  • 为什么我们应该一起使用create和copytruncate?

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