While on the subject of logging with PHP I felt it would be wise to focus discuss some methods of enhancing Apache logging. As anyone who has run an Apache server knows the httpd-access log is pretty much always active which means that if you would like to rotate your log file before it becomes too large for sane processing then you need to stop the http daemon backup and clear the file before relaunching the service. This can be a tedious process and if done manually a royal pain in the backside.
Fortunately the fist option Apache ships with the optional rotatelogs and if you do not believe me try ‘which rotatelogs’ on your command line. On my servers it resides in /usr/local/sbin/rotatelogs but on my desktop it lives in /usr/sbin/rotatelogs.It is rather important to know where the file is so that you can properly reference it in the httpd conf.
The command enables you to pipe in your log output while specifying a time or even size limit. This limit automatically triggers the system rotate the log thus eliminating the need to stop the service as mentioned above. This can be very handy on a production server where even a few nanoseconds of down time could mean the loss of important sales.
The man page for rotatelogs gives to very useful examples;
CustomLog "|/usr/local/sbin/rotatelogs /var/logs/logfile 5M" common
The first line sets the hard file size limit of 5Mb and send the output in the common log format. Remember that you can define a new log format in the httpd.conf or your vhost config file as follows;
LogFormat "%h %l %u %t "%r" %>s %b" SpecialLog
Thus you would then reference that format in lieu of the common one as demonstrated in the example below. Also note the differences of the added date stamp in the file name using local time as well as the rotation limit of 1 day (86400 seconds). Note that the paths have been shortened to improve readability.
CustomLog "|rotatelogs -l logfile.%Y.%m.%d 86400" SpecialLog
Before you start monkeying around with your logs you should take a moment to consider how these change might affect your web stat processing systems. For instance if you are running webalizer then you might want to maintain the consistency of your log file naming and attempt to complete the webalizer processing cycle immediately prior to the rotation.
Another important consideration is if your system is configured for multiple virtualhosts each with it’s own log files. This is a slightly more complex configuration and will also require some additional sophistication in the manipulation of web statistics and log analysis. regardless of your situation these issues can be a bit tricky to facilitate and will likely require you to conduct some experiments in order to get it right.
I hope that this article give you some ideas and promise in the next two article that we will look at a few other logging options like cronolog and syslog consolidation.
Leave a Reply