How to configure logrotate with php logs

Steven De Groote picture Steven De Groote · Jan 3, 2013 · Viewed 13.1k times · Source

I'm running php5 FPM with APC as an opcode and application cache. As is usual, I am logging php errors into a file.

Since that is becoming quite large, I tried to configure logrotate. It works, but after rotation, php continues to log to the existing logfile, even when it is renamed. This results in scripts.log being a 0B file, and scripts.log.1 continuing to grow further.

I think (haven't tried) that running php5-fpm reload in postrotate could resolve this, but that would clear my APC cache each time.

Does anybody know how to get this working properly?

Answer

hakre picture hakre · Jan 3, 2013

I found that "copytruncate" option to logrotate ensures that the inode doesn't change. Basically what is [sic!] was looking for.

This is probably what you're looking for. Taken from: How does logrotate work? - Linuxquestions.org.

As written in my comment, you need to prevent PHP from writing into the same (renamed) file. Copying a file normally creates a new one, and the truncating is as well part of that options' name, so I would assume, the copytruncate option is an easy solution (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.

See Also: