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

For more information on log rotation, please see here.
Try using log rotation (logrotate) (Example of httpd (Apache) configuration) | Tips for setting up and building a rental server or home server

Speaking of which, I wondered how log rotation works in Windows, and when I looked into it, I
found that logs are rotated using "rotatelogs.exe" which comes with Windows Apache, but it seems that logs cannot be deleted.

How to delete old logs after rotation

Check Apache log settings

Set 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 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
change it to "Rotate every 86400 seconds (1 day)."

On Linux, logrotate will even delete the logs if you configure it properly, but
on Windows you'll need to use a little ingenuity from this point on.

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 the conditions.
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 in the Task Scheduler,
you can delete files that are x days old from the rotated files.
also compress old logs by using a compression command (you will need 7zip or similar) instead of the del command
. The combinations are endless.

If you found this article useful, please click [Like]!
2
Loading...
2 votes, average: 1.00 / 12
22,953
X Facebook Hatena Bookmark pocket

The person who wrote this article

About the author