The log rotation periodically divides large files (like access logs) into smaller ones and removes old files.

If you run a web server, like Apache httpd, you probably track your traffic in a file named access_log.

Your website becomes popular and your log now contains thousands of  lines. To analyze the workload in a specific period of time could be  frustrating. The default log rotation is weekly, so all access information will be stored in one file during a 7-day period.

I prefer a daily log file for access_log and error_log. If you also want to change it, you can follow the instruction below.

How to change the logrotation.d:

Open the file logrotate.conf. In this configuration file, you  can change the default logrotation settings. If you wish to set the  values for each log file individually, you can skip this.

nano /etc/logrotate.conf

This should contain something like this:

weekly # rotate log files weekly 
 
rotate 4 # keep 4 weeks worth of backlogs
 
create # create new log files
 
dateext  # use date as a suffix
 
#compress # compress log files or not

...

To change each log individually, open the /etc/logrotate.d/ directory. Here you can see the configuration files for some applications like yum. Open the httpd file:

nano /etc/logrotate.d/httpd
/var/log/httpd/*log {
    daily
    missingok
    notifempty
    sharedscripts
    delaycompress
    postrotate
        /bin/systemctl reload httpd.service > /dev/null 2>/dev/null || true
    endscript
}

I added a new attribute at line 2. You can replace it with weekly or monthly.

With this set, you are ready to go! There is no need to reload, because  the daemon will read the configuration, when the cronjob execute the  script.


Please comment below, if you have any questions.

Tested on:

  • OS: CentOS 7
  • Web server: Apache httpd 2.4.6

Credits: