Rotating Apache log files without killing the server ==================================================== Original by Ben Laurie Enhanced by Z. Wagner - Ice Bear Soft THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. The program is a free software. Everyone is granted permission to use it for any purpose on unlimited number of computers, make copies of this program and distribute it provided no money is required for distribution except of nominal fee of medium, postage, etc. The program should preferably be distributed as a contribution to the Apache software. This brief documentation was written by Z. Wagner. ================================================================== The Apache server writes access and error information to the log files which are open during the whole server session. One day the log files become too large and they consume too much space on the disk. The log files must then be removed. It may also be uncomfortable to view large log files. Moreover, if OS/2 for some reason dies and cannot properly shutdown HPFS disks, it sometimes spoils open files but preserves files which were not in use (it really happened on my server). Thus it is useful to split the log files e.g. on a per day basis which will then preserve as much information as possible. Such operation requires unfortunately killing the server. Ben Laurie wrote a simple "rotatelogs" program which can serve as a pipe for the logging activities. The names of the log files are derived from the number of seconds elapsed from the beginning of the Coordinated Universal Time. This is not intuitive if you wish to find a specific event in the log file. This program goes one step further. The code is almost the same as that of Ben Laurie. The change is in the specification of the log file name. It accepts specifiers valid for the "strftime" function. Usually it will contain %Y-%m-%d in this order so that it can be properly sorted in directory listings. The program accepts two or three arguments. The first one is the template for the log file name, the second one is the rotation time in seconds. The rotation time will mostly be 86400 and the log file name can be specified as access.%Y-%m-%d (of course, you should enter full path in your httpd.conf file). It is possible to rotate the log files in any time interval. If the log files should be rotated every 5 minutes, you can specify: access.%Y-%m-%d-%H-%M 300 The program makes no check whether the arguments make sense. It just creates the file, or opens an existing file in an append mode. If you specify access.%Y-%m-%d 300 zwrotlogs will rotate the log file every 5 minutes but the text will be appended to the same file. The file will actually rotate every day. If you specify access.%Y-%m-%d-%H-%M 86400 the log file will be rotated every midnight. Thus the file name will always end with "-00-00". Unfortunaly, at some version of EMX the local time ceased to work correctly. I am not sure where is the real source of the problem. Therefore I suggest to rotate the log files every hour but keep the file name unique for the whole day, e.g. access.%Y-%m-%d 3600 Months do not have equal length but it is possible to rotate the log files on 1st of each month by specifying access.%Y-%m 86400 The rotatelogs program by Ben Laurie rotates the logs at midnight of Coordinated Universal Time. This program rotates the logs at midnight of the local time provided the TZ environment variable is properly set. Searching for a particular event in the log files is then more convenient. The last argument of zwrotlogs is the LC_TIME locale. It is not needed if the file name consists of numerical form of date and/or time. You will only need it if you wish to use word form in a different locale than your system default. Such setting is necessary in other tasks, e.g. when setting expiration times. My server defaults to the Czech locale and for setting the expiration time for cookies etc. I must locally redefine it to: setlocale(LC_TIME, "En_GB.IBM850"); It is not particularly useful to have the names of days, months etc. in the file names because sorting of directory listings will not be easy. This feature will therefore hardly be used. This program was compiled with EMX/GCC for OS/2 Warp 3.0 or higher. The executable requires EMX 0.9c fix 4. It was tested in OS/2 Warp Connect 3.0 and OS/2 Warp 4.0 but hopefully should work on any platform. It should be compiled using the same rules as rotatelogs by Ben Laurie. The program works well with access log, error log, and SSL log, but CANNOT be used with the rewrite log.