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.

Logrotate: How and Why you should use it properly.

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.


Gaurav Yadav

Gaurav is cloud infrastructure engineer and a full stack web developer and blogger. Sportsperson by heart and loves football. Scale is something he loves to work for and always keen to learn new tech. Experienced with CI/CD, distributed cloud infrastructure, build systems and lot of SRE Stuff.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Are you preparing for DevOps and SRE Interview?

Here is a book for you.


Interview preparation and interview questions for DevOps and SRE






A good reads for last-minute preparations for handling DevOps and SRE interviews at companies like LinkedIn, Visa, Atlassian, etc.