Mihai Surdeanu

Use logrotate to automatically manage your logfiles

Currently, I am using logrotate on all my VPS servers to automatically manage my logfiles and to avoid keeping them indefinitely. What’s the potential problem by keeping them forever? Quite simple and intuitive. At some point, my servers will run out of free disk space and all applications deployed there will be impacted.

Having this in mind, having a tool for automatically manage logfiles is absolutely necessary. Logrotate is a system utility tool specialized in automatic rotation and compression of logs files.

This tool is by default installed in all Ubuntu versions. To double check if you already have this tool installed, you have to use the following command to see it’s current version:

logrotate

If the tool is installed, you will see something similar with next output:

logrotate 3.14.0 - Copyright (C) 1995-2001 Red Hat, Inc.
This may be freely redistributed under the terms of the GNU General Public License

Usage: logrotate [-dfv?] [-d|--debug] [-f|--force] [-m|--mail=command]
        [-s|--state=statefile] [-v|--verbose] [-l|--log=logfile] [--version]
        [-?|--help] [--usage] [OPTION...] <configfile>

Let’s say we have a script that generates hourly logs in a file called output.log, file present under directory /home/user/logs. Without logrotate, this file will became bigger and bigger in time and at some point everything will crash. To fix this approach, we would like to implement a way to keep just logs generated in last X days and to compress old logs to reduce as much as possible the amount of disk space used. Fair enough.

How we can achieve this? Logrotate. The easiest way to achieve it is to modify global configuration file /etc/logrotate.conf:

/home/user/logs/*.log {
    daily
    missingok
    rotate 14
    compress
    create
}

The logs will be rotated daily, only last 14 days will be took into consideration and compression is also activated. Missingok is a directive to not throw any error if the log file is missing.

That’s it! Feel free to use man documentation of this tool to understand all his capabilities.

Mihai

Pasionat de IT. Pasionat de viață. Pasionat de tot ceea ce înseamnă a face o viață mai bună, plină de înțelegere, ajutor reciproc și iubire de aproape.

Adaugă comentariu

Arhive

Arhiva personală