Learn Steps

Logrotate: How and why you should use it properly.

In my on-call duty for the last few years. One thing that comes, again and again, is log cleanup. This happens because of the wrong logrotate or no use of this at all.

When we talk about disk cleanup, this is kind of toil to me. To understand what is toil you can look at the below video. Toil is something that is coming again and again and can be automated.

If logrotate is setup properly you can be saved from these toil jobs. Now let’s have a look at logrotate.

What is Logrotate:

Logrotate is a utility that automatically manages the rotation and compression of log files. This is done so that disk space is not used up properly.

Configurations:

You can find the logrotate configuration in /etc/logrotate.conf and then the separate configs files can be present ar /etc/logrotate.d/*. The location of the second files is mentioned in /etc/logrotate.conf in an include statement and you can change it from there.

Now let’s see how we can add a new configuration for logrotate. For example we want to logrotate the log present in /var/log/test/*.log

Create a file with name /etc/logrotate.d/test with below content

/var/log/test/*.log {
    daily
    missingok
    rotate 14
    compress
    notifempty
    create 0640 www-data www-data
    postrotate
        systemctl reload app-name
    endscript
}

In this config file, we have used many directives. You can look at these directives using man command

man logrotate

Let’s look at the directives used here.

daily: files are rotated every day.
missingok: If the log file is missing, go on to the next one without issuing an error message.
rotate count: number of files to be rotated before being removed.
compress: an older version of files to be compressed.
notifempty: don’t rotate the file if it is empty.
create: file to be created after rotation.
postrotate: tell what to do after postscript. The command which is written between postrotate and endscript is executed.

These were few directives you can read about more in man pages.

This is the end of this article. Please share and subscribe to keep a watch on new articles.