Kahibaro
Discord Login Register

4.6.4 Log rotation

Why Log Rotation Matters

Applications and system services continuously append to their log files. Without rotation:

Log rotation solves this by:

Most Linux systems implement this with logrotate, but some services also have their own built‑in mechanisms.

Basic Log Rotation Concepts

Key ideas you see across implementations:

logrotate Overview

logrotate is the standard log rotation utility on many distributions (especially those using traditional /var/log files).

It is typically:

logrotate runs periodically (often daily), evaluates its rules, and decides which log files need rotation at that time.

logrotate Configuration Structure

A typical setup:

This defines:

Packages place their own files here, defining:

Example global file snippet:

# /etc/logrotate.conf
weekly
rotate 4
create
compress
include /etc/logrotate.d

This means:

Basic logrotate Options

Within a logrotate block, you configure policies for specific log files:

/var/log/myapp/*.log {
    daily
    rotate 7
    missingok
    compress
    delaycompress
    notifempty
    create 0640 myapp myapp
}

Key directives:

How logrotate Performs Rotation

Two main strategies:

  1. Rename + create (default)
    • Current log file is renamed (e.g. app.logapp.log.1)
    • New empty app.log is created
    • The application must reopen the file (via signal or restart) to continue logging

Pros:

Cons:

  1. copytruncate
    • Current log is copied to a new file
    • Original is truncated to size 0

Pros:

Cons:

Choose based on how the specific application behaves and your tolerance for potential minor gaps.

Pre‑ and Post‑Rotation Scripts

Some services must be notified when logs rotate. logrotate allows custom scripts:

/var/log/nginx/*.log {
    daily
    missingok
    rotate 14
    compress
    delaycompress
    notifempty
    create 0640 nginx adm
    sharedscripts
    postrotate
        [ -s /run/nginx.pid ] && kill -USR1 "$(cat /run/nginx.pid)"
    endscript
}

Script blocks:

sharedscripts matters when multiple files match the pattern:

Managing Multiple Versions and Compression

A typical “balanced” rotation policy:

/var/log/app/app.log {
    daily
    rotate 14
    compress
    delaycompress
    missingok
    notifempty
    create 0640 appuser appgroup
}

Behavior:

Date‑based naming might be preferable when you:

Example:

/var/log/app/app.log {
    daily
    rotate 30
    dateext
    dateformat -%Y-%m-%d
    compress
    missingok
    notifempty
}

Resulting files:

Testing and Forcing logrotate

When you modify logrotate configs, you can:

  sudo logrotate -d /etc/logrotate.conf

-d (debug) shows what would happen without making changes.

  sudo logrotate -f /etc/logrotate.conf
  sudo logrotate -f /etc/logrotate.d/myapp

These commands help validate behavior before waiting for the scheduled run.

Integration with cron and systemd

Common ways logrotate is scheduled:

Typical entry: /etc/cron.daily/logrotate which simply runs:

  /usr/sbin/logrotate /etc/logrotate.conf

Some distributions use a timer unit:

You can inspect the last run via systemctl status logrotate.service or check logs (e.g. via journalctl if systemd logging is used).

Application‑Specific Log Rotation

Some services implement their own rotation logic and may not rely entirely on logrotate. Common patterns:

When an application has strong internal log rotation, you might:

Always check the service’s own documentation before designing your rotation strategy.

Common Pitfalls and How to Avoid Them

Designing a Log Rotation Policy

When creating or tuning rotation:

  1. Assess log volume
    • How many MB/GB per day per log?
    • Peak vs off‑peak patterns
  2. Determine retention needs
    • Compliance requirements (e.g. 90 days, 1 year)
    • Operational needs (debugging, forensics)
  3. Decide on naming and compression
    • Numeric vs date extensions
    • Compression format and level
  4. Coordinate with log shipping
    • If logs are sent to a central system, ensure rotation doesn’t break shipping agents (e.g. configure them to follow renamed files or inode changes)
  5. Test under load
    • Force rotation during busy periods
    • Check for missing or duplicated entries
    • Confirm applications continue logging properly

A well‑designed rotation policy balances disk usage, performance, and the ability to investigate issues long after they occurred.

Views: 131

Comments

Please login to add a comment.

Don't have an account? Register now!