How to configure logrotate with php logs

I am running php5 FPM with APC as a cache of operations and applications. As usual, I am logging php errors in a file.

Since this is getting pretty big, I tried setting up logrotate. It works, but after rotation, php continues to register in an existing log file, even if it is renamed. This causes scripts.log to be a 0B file and scripts.log.1 continues to grow.

I think (have not tried) that reloading php5-fpm in postrotate might solve this problem, but it will clear my APC cache every time.

Does anyone know how to do this correctly?

+8
linux php logrotate
source share
2 answers

I found that the "copytruncate" option for logrotate ensures that the index does not change. Mostly what they were looking for [sic!].

This is probably what you are looking for. Adapted from: How does logrotate work? - Linuxquestions.org .

As written in my comment, you need to prevent PHP from writing to the same (renamed) file. Copying the file usually creates a new one, and truncation is also part of the name of these options, so I would suggest that the copytruncate option is a simple solution ( from 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. 

See also:

+5
source share

Another solution I found on my server is to tell php to open logs. I think nginx also has this feature, which makes me think that this should be a fairly common place. Here is my configuration:

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

All Articles