Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apache rotate Access and Error logs Windows

How can I rotate the Apache Access and Error logs on a Window 2000 box?

I include my batch file below as an answer.

Is there a way of doing this directly via the Apache config file? I'm currently using the following customlog command to generate daily logs.

CustomLog '|" "*Apache-Path/bin/rotatelogs.exe" "*Apache-Path/logs/backup/internet_access_%d-%m-%y.log" 86400' combined

like image 839
alimack Avatar asked Dec 10 '08 11:12

alimack


People also ask

How do I rotate Apache logs in Windows?

Unzip the apache download and go to the bin folder, and confirm there is a rotatelogs.exe file. Install the server as either a service, or start from the bin folder, and confirm it works.

How do you rotate a log in Apache?

The log file rotation is implemented using the rotatelogs utility that is provided with httpd. This utility allows you to define when to rotate logs. The rotatelogs utility uses piped logs. This opens an access log file named access_log located at the /var/log/ directory using the log format defined by the name common.

How do I force rotate var log messages?

If you want to rotate /var/log/syslog it needs to be listed in a logrotate config file somewhere, and you just run logrotate . If it rotated recently, then logrotate -f to force it to do it again. So, you need that in a file, normally either /etc/logrotate. conf or as a file snippet in /etc/logrotate.

How do I find the Apache error log?

On a Linux server, you can access Apache error logs from var/log/apache2/error. log. You can then log out the errors from the error log file by writing the following command: sudo tail -f /var/log/apache2/error.


1 Answers

Here's the DOS batch file, modified as annotated. I run it weekly and it keeps 8 weeks of zipped backups. You'll need to install 7 zip.

I haven't parametrised the paths, feel free to.


@echo off

:: Name - svrlogmng.bat
:: Description - Server Log File Manager
::
:: History
:: Date         Authory    Change
:: 22-May-2005  AGButler   Original
:: 14-Jan-2008  AIMackenzie Changed net stops and paths where necessary

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

:: generate date and time variables
for /f "tokens=2,3,4 delims=/ " %%i in ('date /T') do set trdt=%%k%%j%%i
for /f "tokens=1,2 delims=: " %%i in ('time /T') do set trtt=%%i%%j
set nftu=%trdt%%trtt%

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

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

:: change to the apache log file directory
cd /D "D:\Program Files\Apache Software Foundation\Apache2.2\logs\"

:: stop Apache Service, Move log files and restart Apache Service
"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -k stop

echo %nftu% >> access.log
move "D:\Program Files\Apache Software Foundation\Apache2.2\logs\access.log" "D:\Program Files\Apache Software Foundation\Apache2.2\logs\%nftu%_access.log"

echo %nftu% >> error.log
move "D:\Program Files\Apache Software Foundation\Apache2.2\logs\error.log" "D:\Program Files\Apache Software Foundation\Apache2.2\logs\%nftu%_error.log"

"D:\Program Files\Apache Software Foundation\Apache2.2\bin\httpd.exe" -k start

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

:: zip the files
"D:\Program Files\7-Zip\7z.exe" 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

like image 125
alimack Avatar answered Sep 27 '22 18:09

alimack