Log Rotation

Log Rotation With Logrotate

Share

Logrotation is a handy Linux tool to help automate log cleanup based on customisable retention periods.

I began using logrotate when last year I accidentally allowed log files to fill up a VPS of mine, rendering the system unusable. After manually deleting the files, I wanted an automated solution which is what lead me to finding this tool.

Getting started is easy. First install if needed using

Bash
apt install nginx logrotate

Then update configuration files stored at /etc/logrotate.d. The tool will run on a schedule and clean up files based on your configuration.

In my instance, I modified the default settings and set minsize, maxsize and rotation period for Nginx to clean up server log as per the following.

/etc/logrotate.d/nginx
/var/log/nginx/*.log {
        daily
        missingok
        rotate 2
        minsize 10M
        maxsize 50M
        compress
        delaycompress
        notifempty
        create 0640 www-data adm
        sharedscripts
        prerotate
                if [ -d /etc/logrotate.d/httpd-prerotate ]; then \
                        run-parts /etc/logrotate.d/httpd-prerotate; \
                fi \
        endscript
        postrotate
                invoke-rc.d nginx rotate >/dev/null 2>&1
        endscript
}

You can customise the retention policies around on your needs on a per log basis. Rather than document the tool fully, I encourage anyone interested to look at the available options over at die.net.