Docs / Server Management / Log Rotation with Logrotate

Log Rotation with Logrotate

By Admin · Feb 25, 2026 · Updated Apr 25, 2026 · 31 views · 1 min read

Why Rotate Logs?

Without rotation, log files grow indefinitely until they fill your disk. Logrotate compresses, rotates, and removes old log files automatically.

Default Configuration

The main config is /etc/logrotate.conf. Application-specific configs go in /etc/logrotate.d/.

Creating a Custom Config

Create /etc/logrotate.d/myapp:

/var/log/myapp/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 www-data www-data
    sharedscripts
    postrotate
        systemctl reload myapp > /dev/null 2>&1 || true
    endscript
}

Key Directives

  • daily/weekly/monthly — rotation frequency
  • rotate 14 — keep 14 rotated copies
  • compress — gzip old logs
  • delaycompress — wait one rotation before compressing
  • missingok — don't error if log file is missing
  • notifempty — don't rotate empty files
  • create 0640 user group — create new log file with these permissions
  • postrotate/endscript — commands to run after rotation

Test Configuration

# Dry run (shows what would happen)
sudo logrotate -d /etc/logrotate.d/myapp

# Force rotation
sudo logrotate -f /etc/logrotate.d/myapp

Was this article helpful?