Rolling log file without losing any log event

gziplog-rotationlogging

I have a 8GB file called php.log with a running php script logging into it. It is important for me that I log every event, and I want to compress it and empty the current file without stopping the web server.

If I run:

    mv php.log php.log.backup20140305-01
    touch php.log

I will lose some of the data. How can I do this without losing any data?

Best Answer

You'll find it easier to configure logrotate to do the rotation for you. If you create a file called /etc/logrotate.d/php containing something like the following, it'll handle the log rotation automatically. This is just a guide, so make sure to test and customise it before you put it into production.

/path/to/php.log {
    daily  
    missingok              # don't rotate if the file isn't there...
    notifempty             # ...or if it's zero-length
    rotate 30              # keep 30 days' worth of logs
    compress               # gzip the logs, but...
    delaycompress          # ...only after they're over a day old
    create 640 root adm    # permissions with which to create new files
    sharedscripts
    postrotate
        /etc/init.d/apache2 graceful    # or whatever makes your process let go of the log file
    endscript
}

NB: the comments in this extract break logrotate syntax, so make sure to strip them out of your live config file.