Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to configure logrotate with php logs

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?

like image 297
Steven De Groote Avatar asked Jan 03 '13 19:01

Steven De Groote


2 Answers

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:

  • Why we should use create and copytruncate together?
like image 68
hakre Avatar answered Oct 03 '22 22:10

hakre


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
}
like image 25
greg0ire Avatar answered Oct 03 '22 21:10

greg0ire