One gotcha you need to be aware of, (on *NIX at least), if your application/daemon still has the logfile open for writing your rename in line 28/31 will not remove that association. Instead, the application will continue writing to the logfile.1 and will do so until you eventually remove it, so you might end up losing data between the tar and the rm (consider using the Perl builtin unlink here instead BTW). Also, your application might get confused if the file it is writing to (and for which it holds a valid open file descriptor) suddenly vanishes underneath it.
One method of dealing with this is to send a HUP signal to the application (provided you know which process it is and can figure out the process id) after renaming the file. Most long-running daemons take that as a signal to reopen log files and will do the right thing. You could also figure out which processes have an open file descriptor on the log file (by using the *NIX utility lsof for example) and then send a SIGHUP to that process. Or use one of the modules andreas1234567 pointed out, they have methods for dealing with this exact problem.
Lastly, a non-perl method: take a look at logrotate, it's a common utility used for rotating logs with all bells and whistles built in.
In reply to Re: Log Rotation
by tirwhan
in thread Log Rotation
by Danikar
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |