{"canonical_url":"https://engineer.company/glossary/log-rotation/","content_markdown":"# Log rotation\n\n## What it is\n\nLog rotation is the managed transition from one active log file or segment to\nanother. The writer continues with a new active destination and older material\ncan be compressed, retained, exported or removed according to policy. Common\ntriggers include file size and elapsed time. Some logging systems rotate their\nown files, while an external utility such as logrotate can manage files for\ncooperating processes. A journal or a container logging driver can have its own\nstorage limits and its own rotation implementation.\n\nA service that writes plain files might rotate by renaming the active file, then\nopening a new one under the original name. A process with the old file open can\nkeep writing to it after the rename. The rotation procedure needs to tell that\nprocess to reopen its log or use another method supported by the writer. Copies\nand truncation can be used for certain processes, with a small interval in which\nwrites can be lost. Decide what loss is acceptable before selecting such a\nmethod.\n\nRetention is related but defines a different policy. Rotation decides when\nactive writing moves. Retention governs how long old data is available and\nwhether it is compressed or copied elsewhere. A service can rotate large files\noften and still keep several weeks of useful logs if the storage and retention\npolicy permit it. It can also fail to bound storage if each new segment starts a\nfile that is never removed.\n\n## Why it matters\n\nLogs can compete for the storage that databases, services and the operating\nsystem need. A burst of errors can use more space than months of ordinary\ntraffic. A limit gives the host a way to remain within its intended storage\nbudget during that burst. Measure how quickly logs grow under stress and how\nmuch space is required during compression or export. Include startup failures in\nthe exercise because a service that restarts repeatedly can create a large\nvolume of diagnostic output.\n\nInvestigating an incident also depends on what remains. A team may need logs\nfrom the preceding hour, yet a file-count rule under heavy traffic might retain\nonly recent minutes. A stable time requirement calls for capacity and export\npolicies that account for fluctuations in volume. Set any retention policy\naccording to operational purpose and applicable obligations. Confirm that the\nprocedure can find the right source, time interval and identifier after material\nmoves to a rotated file.\n\nA log collector may continue reading the old file until it reaches the end and\nthen watch the replacement. That handoff depends on how the collector identifies\nfiles. A rename, file truncation and deletion can each produce different\nresults. Test the handoff the real system uses and observe delivered records\naround the boundary. Rotation can keep the host below a disk limit while still\nlosing time-sensitive evidence at the destination if the collector never sees\nthe old segment through to the end.\n\n## What it looks like in practice\n\nSet up a non-production service that emits records with incrementing\nidentifiers. Force rotation using the normal mechanism under continuous writes.\nRecord which process owns the active file and which record appears last in the\nold copy and first in the new one. Read the file on disk and the stream\ntransmitted to the collector. Assert continuity or record the allowed loss with\na clear numerical limit. Check permissions and ownership on the new file so the\nservice and the collector can still do their work.\n\nExercise the cases that affect the storage budget. Generate a burst that rotates\nseveral times and stop the external collector for a while. Measure how many\nearlier records remain, whether compression consumes temporary storage and how\nthe system behaves when disk space is tight. Restart the writer and the\ncollector after rotation. For a local journal or a container logging driver, use\nthe runtime's supported control and inspection tools.\n\nOur\n[case study about running public services on a small host](/portfolio/a-whole-company-on-one-small-server/)\ndocuments a resource-constrained deployment and the use of systemd as the\nsupervisor. Long-running services on a host with limited capacity make storage\nbudgets worth testing explicitly. The test above is an illustration of how a\nteam can validate its own rotation and retention policy on a host with that kind\nof constraint.\n\n## Where it goes wrong\n\nDeleting a path may leave disk space occupied if a process still holds an open\ndescriptor. Examine the process's open resources and use its supported reopen or\nrestart procedure. Track the free space the filesystem reports and compare it\nwith the visible files. The discrepancy can reveal why a cleanup that removed a\nlarge filename did not make room for new writes.\n\nAvoid letting two mechanisms manage the same destination. Docker's json-file\ndriver owns its files, and Docker warns that external interaction can interfere\nwith its logging system. Configure the driver's supported limits when it owns\nstorage. Use the application or system logger's own configuration for other\ndestinations. Record the owner of each path and verify that a runtime\nconfiguration change has actually reached the relevant service or container.\n\nRetaining everything can undermine privacy and capacity, while deleting too\nquickly can make an incident impossible to reconstruct. Design limits around the\ndata needed for diagnosis and recovery. Keep potentially sensitive fields out of\nthe log at the point where possible and restrict access to the retained\nmaterial. Test whether the latest records still arrive when storage is near the\nlimit and whether a known older record can be retrieved under the stated\nretention policy.\n\n## Further reading\n\n- [The logrotate manual](https://github.com/logrotate/logrotate/blob/main/logrotate.8.in)\n  covers rotation rules, renaming, copying and truncation.\n- [Docker's JSON file driver](https://docs.docker.com/engine/logging/drivers/json-file/)\n  documents size and count options for container logs.\n- [systemd journal configuration](https://www.freedesktop.org/software/systemd/man/latest/journald.conf.html)\n  explains storage, rotation and retention settings for the journal.\n\n\u003chttps://engineer.company/glossary/log-rotation/\u003e\n","date_modified":"2026-11-24T21:48:58Z","description":"How log rotation controls active files, protects capacity, and affects what investigators can retrieve later.","formats":{"gemtext":"https://engineer.company/glossary/log-rotation/index.gmi","gopher":"https://engineer.company/glossary/log-rotation/gophermap","html":"https://engineer.company/glossary/log-rotation/","json":"https://engineer.company/glossary/log-rotation/index.json","markdown":"https://engineer.company/glossary/log-rotation/index.md","pdf":"https://engineer.company/glossary/log-rotation/index.pdf"},"kind":"page","language":"en","title":"What is log rotation?","url":"https://engineer.company/glossary/log-rotation/","version":1}