How to delete logs using Apache logrotate on Windows

My name is Ito and I am an infrastructure engineer
Log rotation settings for Apache on Linux are configured using logrotate
Please refer to this page for information on log rotation.
Trying out log rotation (logrotate) (httpd (apache) configuration example) | Tips for setting up and building rental servers and home servers
Speaking of which, I wondered how log rotation works on Windows, and after looking into it, it seems that
logs are rotated using "rotatelogs.exe," which is included with Apache on Windows, but it doesn't seem to be possible to delete them.
How to delete old logs after rotation
Check Apache log settings
Configure Apache's log file output settings in "httpd.conf".
The log output settings will be changed daily.
#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 changing the settings 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 we
will change it to "Rotate every 86,400 seconds (1 day)."
In Linux, logrotate will handle log deletion if you configure it correctly, but
in Windows, you'll need to do a bit more.
Create a batch file
You can create a batch file and run it periodically to delete old rotated files
forfiles /P "D:\Apache\logs" /D -7 /C "cmd /c del @file"
The `forfiles` command extracts files that meet certain criteria.
The meaning of each argument is as follows:
| argument | explanation |
|---|---|
| /P | Target path |
| /D | Extract based on file update date (-7 means 7 days ago) |
| /C | Execute another command on the output file (in this case, cmd command) |
| @file | Variable for extracted file (if @ext, it will be a variable representing the extension of the extracted file) |
By creating a batch file like this and registering it with the Task Scheduler,
you can delete files older than x days from the rotated files.
Alternatively, you can compress old logs using a compression command (such as 7zip) instead of the `del` command
. The possibilities are endless!
2
