Always maintaining the same file name for the currently active one is what rotatelogs
is designed to avoid - it opens a new file with a unique but predictable suffix (by default) or name pattern (when you use %
characters) at the predefined interval / condition.
On sites with low visitor counts I'd suggest having a setting:
CustomLog "|/usr/sbin/rotatelogs -c /var/www/html/site1/logs/access.log.%Y.%m" combined
that generates a new log file ever month with the name pattern for August 2023 like access.log.2023.08
.
For sites that generate more traffic: generate a new log file daily (which is what the 86400 option you used did) and use
CustomLog "|/usr/sbin/rotatelogs -c /var/www/html/site1/logs/access.log.%Y.%m.%d " combined
That generates a new log file ever day with the name pattern for August 24th 2023 like access.log.2023.08.24
. (A pattern like that ensures that a ls -l
will sort file in chronologic order and is something that I find much easier to use than for example access.log.24082023
that that's a personal preference.)
Since I now have a predictable filename pattern I can set cron job that runs just after midnight that runs:
#!/bin/bash
basefile="/var/www/html/site1/logs/access.log"
currentlog=$(date +$basefile.%Y.%m.%d)
rm $basefile
ln -s $currentlog $basefile
# and you can do some maintenance here as well, like for example
# compress the log file from a week ago
oldlog=$(date --date="1 week ago" +$basefile.%Y.%m.%d)
gzip $oldlog
Which creates a symbolic link from accesss_log to the the current log file for that day.
You can configure fail2ban with the static filename it expects /var/www/html/site1/logs/access.log
and all is golden.
When you don’t want that you typically configure Apache httpd with:
CustomLog /var/www/html/site1/logs/access.log combined
Then Apache hhtpd will always write log events to /var/www/html/site1/logs/access.log
But then you need to configure an external function to rotate the log file and because Apache httpd opens a file handle and renaming /var/www/html/site1/logs/access.log
to /var/www/html/site1/logs/access.log.old-label
doesn't close that file handle you need restart apache httpd.
When you don't restart Apache httpd all logs evens will continue to be written /var/www/html/site1/logs/access.log.old-label
and not to a new /var/www/html/site1/logs/access.log
You can of course script that yourself, but most people use logrotate
for that rather than reinventing the wheel.