AB's Useful Box - Apache for Win32 Log file Rotation

So you have finished installing Apache for Win32, one of the best web servers you can get on your WinNT / Win2K / WinXP machine, but find that it's log files, access.log and error.log just keep on growing !

Well fear not, it is possible to manage those log files, and automate the process without to much grief.

The following batch file, the was written and tested on WinXP, but should just as well on WinNT and Win2K, should:


@echo off
:: Name - svrlogmng.bat
:: Description - Server Log File Manager
::
:: History
:: Date         Author     Change
:: 22-May-2005  AGButler   Original
:: 04-Mar-2009  AGButler   Updates - thanks to Geoff Brisbine
::

:: ========================================================
:: setup variables and parameters
:: ========================================================

:: generate date and time variables
:: may need to swap around the k j i variables to get the yyyymmdd format
for /F "tokens=6,7,8 delims=/ " %%i in ('echo.^|date^|find "current" ') do set trdt=%%k%%j%%i
:: the following should get the windows time to get the hhmmsstt format
for /F "tokens=5,6,7,8 delims=:. " %%i in ('echo.^|time^|find "current" ') do set trtt=%%i%%j%%k%%l
set nftu=%trdt%%trtt%

:: set the Number Of Archives To Keep
set /a noatk=7


:: ========================================================
:: turn over log files
:: ========================================================

:: change to the apache log file directory
cd "c:\program files\apache group\apache\logs\"


:: stop Apache Service, Move log files and restart Apache Service
net stop apache

echo %nftu% >> access.log
move "c:\program files\apache group\apache\logs\access.log" "c:\program files\apache group\apache\logs\%nftu%_access.log"

echo %nftu% >> error.log
move "c:\program files\apache group\apache\logs\error.log" "c:\program files\apache group\apache\logs\%nftu%_error.log"

net start apache


:: ========================================================
:: zip todays Access and Error log files, then delete old logs
:: ========================================================

:: zip the files
7za a -tzip %nftu%_logs.zip %nftu%_access.log %nftu%_error.log

:: del the files
del /Q %nftu%_*.log


:: ========================================================
:: rotate the zip files
:: ========================================================

:: make list of archive zip files
type NUL > arclist.dat
for /F "tokens=1,2 delims=[] " %%i in ('dir /B *_logs.zip ^| find /N "_logs.zip"') do echo  %%i = %%j>> arclist.dat

:: count total number of files
for /F "tokens=1 delims=" %%i in ('type arclist.dat ^| find /C "_logs.zip"') do set tnof=%%i

:: setup for and create the deletion list
set /a negtk=%noatk%*-1
set /a tntd=%tnof% - %noatk%

type NUL>dellist.dat
for /L %%i in (%negtk%,1,%tntd%) do find " %%i = " arclist.dat >> dellist.dat

:: del the old files
for /F "tokens=3 delims= " %%i in ('find "_logs.zip" dellist.dat') do del /Q %%i

:: remove temp files
del /Q arclist.dat
del /Q dellist.dat


:end

The command line 7za zip utility is available from http://www.7-zip.org

The best method is to just put the 7za.exe file into your system32 folder.

Then it is just a matter of calling the batch file from the windows scheduler. On WinNT this is done on the commend line via AT and on WinXP this is done via Scheduled Tasks found in the Control Panel.

Some other things that can be done after the files have been moved, but before they are zipped, it to produce statistics from the access.log data. For example, how many times pages are being visited and from where.

If all of the above batch file was a little to much, then you could just do the following:


:: ========================================================
:: turn over log files
:: ========================================================

:: change to the apacge log file direcoty
cd "c:\program files\apache group\apache\logs\"

:: stop Apache Service, Delete log files and restart Apache Service
net stop apache

del /Q "c:\program files\apache group\apache\logs\access.log"

del /Q "c:\program files\apache group\apache\logs\error.log"

net start apache

But if you're going to do that, you might as well just turn off logging.


Christoph Sternberg forwarded a much neater method of obtaining the date and time, under WinXP.

Substitute in the following:

:: generate date and time variables
:: may need to swap around the k j i variables to get the yyyymmdd format
set trdt=%date:~6,4%%date:~3,2%%date:~0,2%
:: the following should get the windows time to get the hhmmsstt format
set trtt=%time:~0,2%%time:~3,2%%time:~6,2%
set nftu=%trdt%%trtt%


And could just as easily be:

:: generate date and time variables
set nftu=%date:~6,4%%date:~3,2%%date:~0,2%-%time:~0,2%%time:~3,2%%time:~6,2%

How slick is that! Thanks Christoph.

Updated: 12-May-2009
index