How to configure logrotate with php logs

14,389

Solution 1

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:

Solution 2

Another solution I found on a server of mine is to tell php to reopen the logs. I think nginx has this feature too, which makes me think it must be quite 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
}
Share:
14,389
Steven De Groote
Author by

Steven De Groote

PHP Web Developer and Certified Java Architect

Updated on July 24, 2022

Comments

  • Steven De Groote
    Steven De Groote almost 2 years

    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?

  • Steven De Groote
    Steven De Groote over 11 years
    Perfect! Works like a charm