How to delete logs with Windows Apache log rotation
My name is Ito and I am an infrastructure engineer.
Logrotate is used to configure Apache log rotation on Linux.
For more information on log rotation, please see here.
Try using log rotation (logrotate) (httpd (apache) configuration example) | Tips for setting up and building a rental server/home server
By the way, I looked into how log rotation works in Windows, and found that
logs are rotated using "rotatelogs.exe" that comes with Windows Apache, but it seems that they cannot be deleted.
This is how to delete old logs after rotation.
Check Apache log settings
Configure the Apache log file output settings in "httpd.conf".
Change the log output settings on a daily basis.
#Original settings ErrorLog "|bin/rotatelogs.exe logs/error-%Y%m%d-%H.log 10M -l" CustomLog "|bin/rotatelogs.exe logs/access-%Y%m%d- %H.log 10M -l" common #After setting changes ErrorLog "| bin/rotatelogs.exe logs/error_%Y%m%d.log 86400" CustomLog "| bin/rotatelogs.exe logs/access_%Y%m% d.log 86400" common
The original setting is "Rotate when the log reaches 10MB", so
change it to "Rotate every 86400 seconds (1 day)".
If you are using Linux, logrotate will even delete the logs if you configure the settings, but
if you are using Windows, you will need to do something more.
Create a batch file
It is possible to delete old rotated files by creating a batch file and running the batch periodically.
forfiles /P "D:\Apache\logs" /D -7 /C "cmd /c del @file"
Use the "forfiles" command to extract files that match the conditions.
The meaning of each argument is as follows.
argument | explanation |
---|---|
/P | Target path |
/D | Extract based on file modification date (-7 means 7 days ago) |
/C | Execute another command on the output file (cmd command in this case) |
@file | Variable of the extracted file (@ext is a variable that represents the extension of the extracted file) |
By creating a batch file like this and registering it in the task scheduler,
you can delete files from x days ago from the rotated files.
possible to compress old logs by using a compression command (such as 7zip) instead of the del command The combinations are endless.