Log rotation

On this page 7

Log rotation is the automated practice of closing the current log file, archiving it under a new name and starting a fresh file, combined with compression and deletion of old archives according to a retention policy. In server monitoring and logs, rotation keeps disk usage bounded, keeps individual files small enough to search, and enforces how long access logs, error logs and application logs are kept.

Without rotation, a busy web server can fill a disk with a single ever-growing log file, and a full disk then stops databases, mail queues and the application itself. Rotation is therefore a reliability measure as much as a housekeeping task.

How log rotation works step by step

A rotation cycle performs the same ordered sequence each time a trigger fires. The trigger is usually a schedule, such as daily, or a size threshold, such as 100 MB, and the most common tool on Linux servers is logrotate, run daily by cron or a systemd timer.

  1. Check the trigger: the tool compares file age or size with the configured condition.
  2. Rename the active file: access.log becomes access.log.1, and older archives shift up by one number or receive a date suffix.
  3. Signal the writer: the service is told to reopen its log file, or the tool copies and truncates the file instead.
  4. Compress archives: older files are compressed, often with a one-cycle delay.
  5. Delete expired files: archives beyond the retention count or age are removed.

The third step is the delicate one. A process writes through an open file descriptor, not through a file name, so after a rename it keeps writing into the renamed file until it reopens its log. Services such as nginx reopen on SIGUSR1, while others need a reload or a dedicated command.

Rotation methods and their trade-offs

There are several ways to make a running service move to a new file, and each has different guarantees about lost or duplicated lines. The table compares the methods found on production servers.

MethodHow it worksMain trade-off
Rename and signalRename file, send a reopen signal or reloadClean and lossless if the service supports it
Copy and truncateCopy content, then truncate the original to zeroWorks with any writer but can lose lines written during the copy
Application-managed rotationLogging library rotates its own filesNo external tool, but settings differ per application
Piped loggingService writes to a helper that splits files by timeFlexible, adds a process to manage
Journal-based storagesystemd-journald manages size and retention itselfNo file rotation, uses its own binary format
Log rotation methods compared

The copytruncate option is popular because it needs no cooperation from the application, but it has a small race window and duplicates disk I/O. Where a service supports a reopen signal, rename and signal is the more reliable choice.

Key log rotation parameters

A rotation policy is defined by a handful of parameters that together determine disk usage, search convenience and how much history survives. The values below are typical starting points, not fixed rules.

DirectiveTypical valueEffect
daily / weeklyDaily for busy web logsTime-based rotation trigger
size or maxsize50-500 MBRotates early when a file grows too fast
rotate7-90 archivesNumber of old files kept
compress / delaycompressEnabledSaves space, keeps the newest archive readable
dateextEnabledUses dates instead of numbers in archive names
missingok / notifemptyEnabledAvoids errors and empty archives
Common logrotate directives and typical values

Text logs typically compress to a small fraction of their original size, often around a tenth, because they are highly repetitive. That ratio makes it practical to keep weeks of history locally, while long-term retention usually moves to a central log platform.

Rotation is one part of a log lifecycle, and it is often mixed up with retention and shipping. The distinctions matter when designing compliance policies or debugging missing data.

ConceptQuestion it answersRelation to rotation
Log rotationWhen does a new file startLocal file management on the server
Retention policyHow long logs are keptRotation enforces it locally
Log shippingWhere logs are copiedAgents must handle rotated files correctly
Log aggregationWhere logs are searchedCentral store has its own retention
Log samplingHow much is recordedReduces volume before rotation
Rotation and neighboring log management concepts

Log shipping agents follow files by inode and offset, so they detect a rename and continue reading the rotated file until its end. A misconfigured agent that follows only the file name may skip the last lines before each rotation.

Common log rotation mistakes

  • Rotating without reopening: the service keeps writing into access.log.1, which is then compressed or deleted, and new entries vanish.
  • Deleted but open files: a log removed while still open keeps consuming disk space until the process restarts, so df and du disagree.
  • Retention set by habit: privacy law and security standards may require minimum or maximum retention periods, so the count should follow policy.
  • Forgotten application logs: custom logs outside /var/log are often missing from the rotation configuration and grow unchecked.
  • Wrong permissions on new files: the create directive must set the owner and mode the service expects, or logging fails silently.

In plain words

A server writes a diary of everything it does, and that diary never stops growing. Log rotation closes the current notebook at a set time, puts it on a shelf and opens a new one.

Older notebooks are shrunk to save space, and the oldest are thrown away after a set period. The server always has recent history at hand without letting the diary fill the whole room.

A real-world example

A hosting provider operates web servers that each write about 2 GB of access logs per day. Twice in one month a server ran out of disk space overnight, which stopped the database and took several customer sites offline.

The operations team configures logrotate for daily rotation, an additional size limit of 500 MB, 14 compressed archives, delaycompress and a postrotate command that sends nginx the reopen signal. Local log storage stabilizes at a predictable level, logs older than two weeks are available in the central log platform, and no further disk-full incidents occur.