Our PHP Log files are getting huge, and we need a utility to regularly rotate the file. This is on a Windows 2003 Server.
Is there a program / script similiar to Linux's logrotate
?
And yes, we are busy overhauling the PHP scripts to remove all of the errors, notices and warnings.
If you are using Apache to run PHP then there is a built-in utility called rotatelogs that you can pipe your logs through and it will automatically do the log rotation for you. Here is how you use it on Windows:
Another alternative is to set the PHP error log location in run-time to a file that includes the date, i.e. add this to your prepend file:
I know your pain. Several years ago, I ended up reading the source for logrotate, and ported it to Windows (translating from C to Perl in the process -- yes, I'm a glutton for punishment).
The main problem with Windows log-rotation is file-locking. If a file is open during the rotation, it can't be moved, renamed etc. I got around this, by deciding that for my application, the rotation was more important than a few missed log-lines and simply doing a copy, then truncate (though, I'm sure that there was more to it than that, unfortunately I don't have the source readily available). In any case, it turned out that the operation was so quick, that I very much doubt that we've lost more than a couple of lines in the history of its usage.
This is, I believe, why native Windows applications generally do their own log-rotation -- the application itself holds the locks and can cache the log-messages while the rotation occurs, for later flushing.
The best method, in my experience, is to disiable the internal PHP log and have it log to the Windows error log (or syslog on unix hosts).
Once there ntsyslog or similar can punt logs to a *nix host where tools like logwatch make filtering easy.
I don't use PHP, but with SQUID I have a batch file that runs at midnight and stops SQUID, renames the log file and restarts SQUID. To generate the file name use something like:
This is UK date format where the day is the first two characters. You can pick out any bits of the date just by changing the second line.
JR
I use the "rnanalog" utility to rotate my Perforce logs, I use the command:
in a batch file and simply run this at midnight via a scheduled task.
If file locking is an issue then you may have to stop the service for a few seconds while the log is rotated...