The Apache HTTP Server does not rotate log files.
On Red Hat Linux, Oracle Linux etc. rotation of the Apache log files are handled by cron.daily and logrotate.
On Windows it is a bit more difficult because of the way open files are locked. You have two options, either stop Apache and do the rotate yourself or use an Apache feature called Piped Logs.
Instead of writing to log files Apache can write to a pipe. Another program can then read from the pipe and handle the logging.
The Apache HTTP Server includes a rotatelogs executable that can be used.
Below are some examples of how you use piped logs and rotatelogs.
#ErrorLog "logs/error.log" ErrorLog "|bin/rotatelogs.exe -l D:/Apache2.2/logs/error.%Y.%m.%d.log 86400" #CustomLog "logs/access.log" common CustomLog "|bin/rotatelogs.exe -l D:/Apache2.2/logs/access.%Y.%m.%d.log 86400" common #TransferLog "D:/Apache2.2/logs/ssl_access.log" TransferLog "|bin/rotatelogs.exe -l D:/Apache2.2/logs/ssl_access.%Y.%m.%d.log 86400" #CustomLog "D:/Apache2.2/logs/ssl_request.log" \ # "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b" CustomLog "|bin/rotatelogs.exe -l D:/Apache2.2/logs/ssl_request.%Y.%m.%d.log 86400" \ "%t %h %{SSL_PROTOCOL}x %{SSL_CIPHER}x \"%r\" %b"
This will result in log files named like this:
error.2013.08.15.log
access.2013.08.15.log
ssl_access.2013.08.15.log
ssl_request.2013.08.15.log
A new log file will be created every 24 hours.
Now all you have to do is to find a way to delete old log files.
I am no PowerShell expert but this get the job done.
Get-ChildItem 'D:/Apache2.2/logs/*.log' | Where {$_.lastwritetime -lt (Get-Date).AddDays(-30)} | Remove-Item -Force
While you test you can use the -WhatIf parameter. It will tell you which files will be deleted but it will not delete anything:
Get-ChildItem 'D:/Apache2.2/logs/*.log' | Where {$_.lastwritetime -lt (Get-Date).AddDays(-30)} | Remove-Item -Force -WhatIf
{ 33 comments… read them below or add one }
Hi Peter
thanks for sharing this….I have been running all around for this and finally found it here….But its not working me, please suggest, I have all 3 ways but still get an error message while starting apache “>>> ErrorLog takes one argument, The filename of the error log” in windows event log.
I tried all these but no luck..
ErrorLog “|bin/rotatelogs.exe -l “C:/Program Files/CollabNet/Subversion Server/httpd/logs/error.%Y.%m.%d.log” 86400″ common
ErrorLog “|bin/rotatelogs.exe -l C:/Program Files/CollabNet/Subversion Server/httpd/logs/error.%Y.%m.%d.log” 86400 common
ErrorLog “C:\Program Files\CollabNet\Subversion Server\httpd\bin\rotatelogs.exe -l C:/Program Files/CollabNet/Subversion Server/httpd/logs/error.%Y.%m.%d.log” 86400 common
Thanks again
Hi,
You use:
ErrorLog “|bin/rotatelogs.exe -l “C:/Program Files/CollabNet/Subversion Server/httpd/logs/error.%Y.%m.%d.log” 86400″ common
It should be:
ErrorLog “|bin/rotatelogs.exe -l C:/Program Files/CollabNet/Subversion Server/httpd/logs/error.%Y.%m.%d.log 86400” common
But this will probably still not work because there is a space in the path. You can create symlink and use that instead.
For example:
C:/Program Files/CollabNet
mklink /d SubversionServer “Subversion Server”
Regards Peter
thanks Peter for such a quick response…
does that mean i need to create symlink for Program Files aswell, Let me try and will update you..
Yes sorry did not notice there were two spaces in the path.
You made my day Peter.. created one symlink as suggested by you and works now…..I hope my log rotation works now…thanks once again…wish you all the best
just an update ..for symlink i tried mutiple commands …but this one worked
mklink /D SubversionServer “c:Subversion Server”
Hi Peter,
First of all thanks, I too was searching for this for ages.
I had the same issue as Gill with spaces in the path and by trial and error managed to get it to work by just enclosing the path in single quotes, so using Gill’s path it would be :-
ErrorLog “|bin/rotatelogs.exe -l ‘C:/Program Files/CollabNet/Subversion Server/httpd/logs/error.%Y.%m.%d.log’ 86400″ common
Hi,
I have implemented log rotation in Windows server 2003 R2 as shown below, but it is not working.
CustomLog “| E:/apache/bin/rotatelogs.exe -l E:/apache/logs/access.%Y.%m.%d.log 86400” combined
ErrorLog “|E:/apache/bin/rotatelogs.exe -l E:/apache/logs/error.%Y.%m.%d.log 86400”
Could you please help.
Thanks in advance.
Hi Hari,
It looks like you are using the wrong quotes character. You should use " not “ or ”. But it might just be a copy/past or display error 🙂
Regards Peter
Dear Peter,
Thank you for your prompt response.
I was trying with the correct quotes as below:
ErrorLog “|E:/apache/bin/rotatelogs.exe -l E:/apache/logs/error.%Y.%m.%d.log 86400”.
I dint do copy/paste.
Please suggest me any other possible errors
could you please clarify me that, whether we can implement rotate logs function independent of the version of apache .
Requesting your kind help to troubleshoot the issue.
Thanks
Hari.
I have only used this for version 2.2.20 and above. Which error do you get? Can you see anything in the error log? Can you see anything in the Windows Event log?
Hi Peter
Apologies for late reply.
I checked event log and error log , i dont see any errors.
Firstly the logs are not updating if i edit the the httpd.conf file as below.
As shown below, in server1 and server2 the rotation has been completed, but in server3 and 4 it is not done.
Kindly help
==================================================
server1
D:\wamp\bin\apache\Apache2.2.11\conf
ErrorLog “|bin/rotatelogs.exe -l E:/wamp/logs/apache_error.log.%Y.%m.%d.log 86400”
#CustomLog “e:/wamp/logs/access.log” common
CustomLog “|bin/rotatelogs.exe -l E:/wamp/logs/apache_access.log.%Y.%m.%d.log 86400” common
==================================================================
server2
#ErrorLog “D:/wamp/logs/apache_error.log”
ErrorLog “|bin/rotatelogs.exe -l D:/wamp/logs/apache_error.log.%Y.%m.%d.log 86400”
CustomLog “|bin/rotatelogs.exe -l D:/wamp/logs/apache_access.log.%Y.%m.%d.log 86400” common
======================================================
server3
E:\Apache\conf
ErrorLog logs/error.log
ErrorLog “|bin/rotatelogs.exe -l E:/Apache/logs/apache_error.log.%Y.%m.%d.log 86400”
CustomLog “|bin/rotatelogs.exe -l E:/Apache/logs/apache_access.log.%Y.%m.%d.log 86400” common
========================================
server4
E:\apache\conf
ErrorLog logs/error.log
ErrorLog “|bin/rotatelogs.exe -l E:/apache/logs/apache_error.log.%Y.%m.%d.log 86400”
CustomLog “|bin/rotatelogs.exe -l E:/apache/logs/apache_access.log.%Y.%m.%d.log 86400” common
==============================
Hi Peter,
Do you know why i have a cmd.exe window opening when i use rotatelogs for access ??
Can’t close it.
Thanks for response.
Yvon
Sorry Yvon,
I don’t know. I can see others have had the same problem but I have not found any solution.
Regards Peter
Hi Hari,
The only idea I have is to input the full path to the rotatelogs.exe, to see if that makes a difference.
Regards Peter
Hi Peter,
I have tried even with full path to the rotatelogs.exe, but that doesnt work out.
K, fine..Thank you peter.
Could this work? To overwrite weekly and limit the growth?
CustomLog “|bin/rotatelogs.exe -l logs/access.%A.log 15M” common
Sorry Tilo I have never tried that. I do not work much on Windows and I have only tried what I put in the post 🙂
Regards Peter
never mind, log will not get overwritten it seems
https://issues.apache.org/bugzilla/show_bug.cgi?id=51081
I think this tool can really be useful to delete log or obsolete files from your servers:
https://sourceforge.net/projects/autodeletions/
Thanks for the tip Alberto
can you help me with aapche server log file for 2-3 months, i need it for my research..
thanx in advance
lucky
Thanks Peter 🙂
Really appreciate what you shared, very helpful, but there’s an odd issue for me now. The logs are rotating every 24 hours, but the start and end time in the logs are 10:00:00 AM and 9.59.59 AM respectively. This does not even match with the time when apache services were restarted for the change to come into effect (around 2AM). Any suggestions?
Well just explored a bit further and found -l missing in my command 🙂 -l uses local time rather than GMT.
CustomLog “|bin/rotatelogs -l /var/logs/logfile.%Y.%m.%d 86400” common
http://httpd.apache.org/docs/2.2/programs/rotatelogs.html
I have configured the log rotation as below
#CustomLog logs/access.log common
CustomLog “|bin/rotatelogs.exe -l logs/access.%Y.%m.%d.log 86400” common
But the logs are rotated by the oldest date with the filename ‘access.log’ and recent log as eg: access.2014.06.24.log (of today) but i think it should the opposite.
Need assistance please
Hi Abdul,
The access.log file is the original file and should be ignored.
Regards Peter
Hi Peter,
Thanks for posting wonderful info.
Errorlog works
ErrorLog “|bin/rotatelogs.exe -l D:/Dev/Apache2.2/logs/error.%Y.%m.%d.%H_%M_%S.log 60”
Not sure why but Custom log is not working.
CustomLog “|bin/rotatelogs.exe -l D:/Dev/Apache2.2/logs/access.%Y.%m.%d.%H_%M_%S.log 60” common
Even though my access.log file is of 8 MB.
Once I update the httpd.conf then I do restart my Apache Services after that I do that Error.log file gets updated but it doesn’t create the access.log file.
But when I put below entry alone.
ErrorLog “|bin/rotatelogs.exe -l D:/Dev/Apache2.2/logs/access.%Y.%m.%d.%H_%M_%S.log 60” it creates the access.log file with timestamp.
Could you please help me to setup this.
Thanks in advance.
Regards,
Vikram
Hi Vikram,
I think the access log is first created when there is a request to the server.
Regards Peter
Hi Peter,
Thank for the tutorial. Every thing is working fine. I made it to work in two servers and in my local PC.
But, I had a issue when tried for the next time.
Once configuring httpd.conf, Apache does creates rotated log files for both access.log and error.log after restarting, but it does not creates anymore log in the actual error.log file, whereas the logs is updated in the rotated log file.
Only after removing rotate log command for Error Log from httpd.conf, again it works fine as usual.
Please Help.
[Do you know why i have a cmd.exe window opening when i use rotatelogs for access ??
Can’t close it.
Thanks for response.
Yvon]
Hi I had the same problem in a Windows 2003 r2, I found a property in the service Apache 2.2. If you use the local system account to run the service, check if you have a tick in “Allow service to interact with desktop”, uncheck this option. Works in my case.
Regards.
P.d: Sorry for my english 😛
Works Great, thanks
Thank you for sharing – everything in one place – saved my day. Thumbs up!
Thanks for your precious informations about logrotate under Windows 🙂