# Log rotation

## What it is

Log rotation is the managed transition from one active log file or segment to another. The writer continues with a new active destination and older material can be compressed, retained, exported or removed according to policy. Common triggers include file size and elapsed time. Some logging systems rotate their own files, while an external utility such as logrotate can manage files for cooperating processes. A journal or a container logging driver can have its own storage limits and its own rotation implementation.

A service that writes plain files might rotate by renaming the active file, then opening a new one under the original name. A process with the old file open can keep writing to it after the rename. The rotation procedure needs to tell that process to reopen its log or use another method supported by the writer. Copies and truncation can be used for certain processes, with a small interval in which writes can be lost. Decide what loss is acceptable before selecting such a method.

Retention is related but defines a different policy. Rotation decides when active writing moves. Retention governs how long old data is available and whether it is compressed or copied elsewhere. A service can rotate large files often and still keep several weeks of useful logs if the storage and retention policy permit it. It can also fail to bound storage if each new segment starts a file that is never removed.

## Why it matters

Logs can compete for the storage that databases, services and the operating system need. A burst of errors can use more space than months of ordinary traffic. A limit gives the host a way to remain within its intended storage budget during that burst. Measure how quickly logs grow under stress and how much space is required during compression or export. Include startup failures in the exercise because a service that restarts repeatedly can create a large volume of diagnostic output.

Investigating an incident also depends on what remains. A team may need logs from the preceding hour, yet a file-count rule under heavy traffic might retain only recent minutes. A stable time requirement calls for capacity and export policies that account for fluctuations in volume. Set any retention policy according to operational purpose and applicable obligations. Confirm that the procedure can find the right source, time interval and identifier after material moves to a rotated file.

A log collector may continue reading the old file until it reaches the end and then watch the replacement. That handoff depends on how the collector identifies files. A rename, file truncation and deletion can each produce different results. Test the handoff the real system uses and observe delivered records around the boundary. Rotation can keep the host below a disk limit while still losing time-sensitive evidence at the destination if the collector never sees the old segment through to the end.

## What it looks like in practice

Set up a non-production service that emits records with incrementing identifiers. Force rotation using the normal mechanism under continuous writes. Record which process owns the active file and which record appears last in the old copy and first in the new one. Read the file on disk and the stream transmitted to the collector. Assert continuity or record the allowed loss with a clear numerical limit. Check permissions and ownership on the new file so the service and the collector can still do their work.

Exercise the cases that affect the storage budget. Generate a burst that rotates several times and stop the external collector for a while. Measure how many earlier records remain, whether compression consumes temporary storage and how the system behaves when disk space is tight. Restart the writer and the collector after rotation. For a local journal or a container logging driver, use the runtime's supported control and inspection tools.

Our case study about running public services on a small host documents a resource-constrained deployment and the use of systemd as the supervisor. Long-running services on a host with limited capacity make storage budgets worth testing explicitly. The test above is an illustration of how a team can validate its own rotation and retention policy on a host with that kind of constraint.

## Where it goes wrong

Deleting a path may leave disk space occupied if a process still holds an open descriptor. Examine the process's open resources and use its supported reopen or restart procedure. Track the free space the filesystem reports and compare it with the visible files. The discrepancy can reveal why a cleanup that removed a large filename did not make room for new writes.

Avoid letting two mechanisms manage the same destination. Docker's json-file driver owns its files, and Docker warns that external interaction can interfere with its logging system. Configure the driver's supported limits when it owns storage. Use the application or system logger's own configuration for other destinations. Record the owner of each path and verify that a runtime configuration change has actually reached the relevant service or container.

Retaining everything can undermine privacy and capacity, while deleting too quickly can make an incident impossible to reconstruct. Design limits around the data needed for diagnosis and recovery. Keep potentially sensitive fields out of the log at the point where possible and restrict access to the retained material. Test whether the latest records still arrive when storage is near the limit and whether a known older record can be retrieved under the stated retention policy.

## Further reading
* The logrotate manual covers rotation rules, renaming, copying and truncation.
* Docker's JSON file driver documents size and count options for container logs.
* systemd journal configuration explains storage, rotation and retention settings for the journal.

=> /portfolio/a-whole-company-on-one-small-server/ case study about running public services on a small host
=> https://github.com/logrotate/logrotate/blob/main/logrotate.8.in The logrotate manual
=> https://docs.docker.com/engine/logging/drivers/json-file/ Docker's JSON file driver
=> https://www.freedesktop.org/software/systemd/man/latest/journald.conf.html systemd journal configuration

=> https://engineer.company/glossary/log-rotation/ This page on the web
