Glossary · L

Log Rotation

The routine of closing, renaming, compressing and eventually deleting log files so that logs stay manageable and do not fill up the disk.

By Obseria TeamUpdated

Definition

Log rotation is the process of regularly closing the current log file, renaming or archiving it and starting a new one, usually combined with compression and deletion of old files. Rotation is triggered by time, such as daily, or by size, such as 100 MB. It prevents logs from filling the disk, but it has to be coordinated with the writing application and with log shippers so that no lines are lost or read twice.

What is log rotation?

Applications and servers write logs continuously. Without intervention, a single log file grows until the disk is full, and a full disk can stop databases, crash applications or prevent the system from writing anything at all. Log rotation solves this by closing the active file at regular intervals, keeping a limited number of older files and deleting the rest.

A typical rotation turns app.log into app.log.1, the previous app.log.1 into app.log.2.gz and so on, until the oldest file is removed. The application then continues writing into a fresh app.log.

Triggers and policies

  • Time-based: rotate hourly, daily, weekly or monthly. Easy to reason about and matches how people search logs.
  • Size-based: rotate when a file reaches a limit, such as 100 MB. Protects the disk better on hosts with bursty log volume.
  • Number of files to keep: defines how much history stays on the host, for example seven daily files.
  • Compression: older files are compressed, often with gzip, which saves a lot of space for text logs.

Local rotation is not the same as retention in a central log platform. Rotation protects the disk of a single host; retention defines how long logs remain searchable centrally, and it is often driven by compliance or audit requirements.

Example: logrotate on Linux

On most Linux distributions, the logrotate utility handles rotation, typically started daily by cron or a systemd timer. Configurations live in /etc/logrotate.conf and /etc/logrotate.d/. An illustrative configuration for an application:

/var/log/myapp/*.log {
    daily
    rotate 7
    maxsize 100M
    compress
    delaycompress
    missingok
    notifempty
    create 0640 myapp myapp
    postrotate
        systemctl kill -s HUP myapp.service
    endscript
}

delaycompress leaves the most recent rotated file uncompressed for one cycle, which helps log shippers that are still reading it. The postrotate script tells the application to reopen its log file. NGINX, for example, reopens its logs on the USR1 signal or with nginx -s reopen.

Rename and reopen vs. copytruncate

There are two basic strategies. With rename and reopen, the file is renamed and the application is signalled to open a new file. This is clean, but the application must support it; otherwise it keeps writing into the renamed file. With copytruncate, logrotate copies the file and then truncates the original, so the application can keep its file handle. The drawback: lines written between the copy and the truncation can be lost, and readers must handle the file suddenly shrinking.

Log rotation in Docker and Kubernetes

Containers usually write to standard output, and the runtime stores those streams in files on the node. Docker's default json-file driver does not limit file size unless you configure it, for example in /etc/docker/daemon.json:

{
  "log-driver": "json-file",
  "log-opts": { "max-size": "50m", "max-file": "5" }
}

In Kubernetes, the kubelet rotates container logs based on the settings containerLogMaxSize and containerLogMaxFiles. Because only a few rotated files are kept per container, logs of a busy or crash-looping pod can disappear from the node within minutes. Shipping them to a central store is the only reliable way to keep them.

Common pitfalls

  • Lost lines when a log shipper does not follow renamed files or when copytruncate races with writes.
  • Duplicate lines when a shipper mistakes a rotated file for a new one and reads it again from the beginning.
  • Deleted but still open files: a process that keeps writing into a deleted file still consumes disk space, which only shows up in tools such as lsof.
  • Rotation that never runs, for example because of a syntax error in a config file or a disabled timer.
  • Debug logging left on, which produces far more volume than the rotation policy was designed for. A suitable log level matters as much as rotation.

Modern log collectors are built for rotation. The OpenTelemetry Collector's filelog receiver, for example, identifies files by a fingerprint of their first bytes rather than only by name, so it can follow a file after it has been renamed.

The observability perspective

In an observable system, local log files are a buffer, not the archive. Logs are collected continuously by an agent or collector, enriched with service, host and trace context, and stored centrally through log aggregation. Rotation can then be short and conservative on each host, while search and retention happen in one place. It is also worth watching rotation itself: disk usage of log volumes, gaps in the log stream of a service and sudden jumps in volume often reveal problems with logging before anyone goes looking for a missing line.

How Obseria helps

Obseria collects logs through the Obseria Agent and OpenTelemetry Collectors and stores them centrally in Obseria Lake, so the history you need does not depend on how many rotated files a host keeps. Live Tail shows incoming lines in real time, which is a quick check that collection still works after a change to rotation. Obseria Pipeline filters and reduces logs before they are stored, and agent management shows health, version and data flow of the installed agents and collectors, so a host that stopped sending logs stands out. With host metrics in metrics monitoring, an alert rule on free disk space warns before a growing log file fills the disk. Rotation itself stays with your operating system, container runtime or kubelet.

Frequently asked questions

Log rotation manages files on a single host so they do not fill the disk. Log retention defines how long logs are kept and searchable, usually in a central log platform, often based on legal or audit requirements.

Many teams combine both: rotate daily for predictable files and add a size limit so that a sudden burst of log lines cannot fill the disk before the next scheduled rotation.

Only when the application cannot reopen its log file on a signal. Copytruncate keeps the file handle intact, but lines written between copying and truncating can be lost. Rename and reopen is the safer default.

Yes. Logs still land on the local disk first, so rotation is needed to protect it. Central collection allows you to keep the local rotation short, because the long-term history lives in the central store.

Put these concepts into practice

Try Obseria free for 14 days, with OpenTelemetry and no proprietary agents.