3

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.

5 Answers 5

3

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');
1
  • ini_set(), it's so simple! gahhhh! Thank you.
    – MonkeyZeus
    Oct 21, 2015 at 21:02
0

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.

0

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.

0

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 THEDAY=%THEDATE:~0,2%
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.

JR

0

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .