Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question

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:

ErrorLog "|bin/rotatelogs.exe -l logs/error/error.%Y-%m-%d.log 86400"

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:

ini_set('error_log', 'path_to_logs/php_error.'.date('Y-m-d').'.log');
share|improve this answer
ini_set(), it's so simple! gahhhh! Thank you. – MonkeyZeus Oct 21 '15 at 21:02

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.

share|improve this answer

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.

share|improve this answer

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:

for /f "tokens=1" %%i in ('date /t') do set THEDATE=%%i
set FILENAME=Squid-%THEDAY%.log

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.


share|improve this answer

I use the "rnanalog" utility to rotate my Perforce logs, I use the command:

C:\Support\Scripts\rnanalog.exe C:\Data\p4journal\audit.log C:\Data\p4backups\audit.logs\audit_mmmddccyy.log

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...

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.