Managing Logs with Logrotate on Centos 7

Configuring Logrotate on Centos7 to Manage System and Application Logs

Logrotate is one of those “set it and forget it” tools every admin should have in their toolkit. Proper log rotation not only keeps your disk from filling up but also makes troubleshooting easier. Whether you’re running on a VPS or managing projects on a dedicated server, setting up logrotate helps ensure system stability and predictable service behavior.

In this guide, we’ll go through how to set up logrotate for both system and application logs.


Checking if logrotate is installed

Before setting up log rotation, make sure the logrotate utility is installed and working correctly. It's usually already present on CentOS 7. To check, use the command:

logrotate --version

You should see something like:

logrotate 3.18.0

If you get a version number, you’re good to proceed.


Where the configuration lives

Logrotate relies on two main configuration sources:

  • /etc/logrotate.conf — the global config file that defines default rotation rules.
  • /etc/logrotate.d/ — a directory where individual apps and services keep their own rotation configs.

Together, these files tell logrotate what to rotate, how often, how many backups to keep, and whether to compress logs.


Inside /etc/logrotate.conf

The file /etc/logrotate.conf defines general rotation parameters. It can be opened with any text editor, such as vi:

vi /etc/logrotate.conf

Example contents:

# Rotate logs weekly
weekly
# Keep 4 old archives
rotate 4
# Create a new empty log file after rotation
create
# Append the date to rotated log filenames
dateext
# Uncomment to compress old logs
#compress
# Include per-application configs
include /etc/logrotate.d
# System log settings
/var/log/wtmp {
monthly
create 0664 root utmp
minsize 1M
rotate 1
}
/var/log/btmp {
missingok
monthly
create 0600 root utmp
rotate 1
}

This file defines the defaults. If a per-service config doesn’t override them, these values will be applied automatically.


Common directives

Here are some of the most frequently used options in logrotate configs:

  • daily / weekly / monthly / size — set rotation frequency by time or file size.
  • rotate N — how many old log archives to keep.
  • create — create a fresh log file after rotation with the same permissions and owner.
  • compress — compress rotated logs (typically with gzip).
  • dateext — append the date to rotated log names (e.g., access.log-20250918).
  • missingok — don’t complain if the log file doesn’t exist.
  • notifempty — skip rotation if the log file is empty.
  • postrotate … endscript — run commands after rotation (e.g., reload a service).
  • maxage N — delete archives older than N days.

Example: application-specific config

Say your app my-app writes logs to /var/log/my-app/. Create a dedicated config file:

vi /etc/logrotate.d/my-app

Config example:

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

What this does:

  • Rotates logs daily.
  • Keeps 14 old copies.
  • Compresses old logs.
  • Skips empty log files.
  • Creates new logs with 0640 permissions, owned by www-data.
  • Reloads my-app after rotation so it picks up the new log file.

Testing your configuration

Debug mode (no changes applied):

sudo logrotate /etc/logrotate.conf --debug

Test just one config file:

sudo logrotate -d /etc/logrotate.d/my-app

Force a rotation immediately:

sudo logrotate /etc/logrotate.conf --force

Rotating logs outside of /var/log

If your app writes logs somewhere else, say /home/user/logs/, create a custom config file:

vi /home/user/logrotate.conf

Example:

/home/user/logs/*.log {
hourly
missingok
rotate 24
compress
create
}

Make a test setup:

mkdir ~/logs
touch ~/logs/access.log

Run rotation manually:

logrotate /home/user/logrotate.conf \
--state /home/user/logrotate-state \
--verbose --force
  • --state defines where logrotate saves its state.
  • --verbose prints details of the rotation process.

Automating with cron

Logrotate is typically run by cron, but you can add custom jobs for your own configs.

Edit your crontab:

crontab -e

Example: run logrotate every hour at minute 14:

14 * * * * /usr/sbin/logrotate /home/user/logrotate.conf --state /home/user/logrotate-state
Need help?Our engineers will help you free of charge with any question in minutesContact us