Log rotation and retention
Quick answer
Rotation replaces or renames a growing log so old records can be retained or removed according to policy. Retention is a balance between diagnostic needs, disk capacity and privacy.
Prepare the practice environment
Run python3 lab-fixtures/logs-time/setup.py from the extracted package root as an ordinary user. Enter the printed path and check pwd. Use a fresh fixture and Bash with LC_ALL=C and set -o pipefail for each example. Never install the sample schedules, change system time or use the global logrotate configuration. Manual inspection requires an authorized disposable VM.
How it works
logrotate evaluates policies when invoked, not continuously. daily sets a time condition, rotate 3 retains up to three rotated versions, compress compresses rotated logs, and missingok tolerates absent files. A daemon keeping an open file descriptor may continue writing to a renamed file until it reopens it; copytruncate has a data-loss window. Journald manages its own retention separately. Debug mode shows planned actions without rotating logs or updating state. Use an explicit private configuration and state path, never the system-wide configuration in this lab.
For example, rotate 3 means up to three retained rotated versions under that rule, not a promise of exactly three days of history. If the application keeps the old file open after a rename, records can still go to the old inode. A private debug run can reveal the proposed rotation decision; check its exit status and diagnostics as well as the unchanged log and state files before concluding validation succeeded.
Try the examples
The rules will be placed inside a stanza for a specific log.
Expected stdout:
dailyrotate 3compressmissingok{ printf '"%s/app.log" {' "$PWD"; cat rotate.rules; printf '}'; } > lab.conflogrotate -d -s "$PWD/rotate.state" lab.conf > debug.log 2>&1test ! -e rotate.state && printf 'state untouched' Read debug.log locally; its wording depends on version and time. No rotation occurs.
Expected stdout:
state untouchedCount records before choosing a retention strategy; bytes and rate also matter.
Expected stdout:
4Common mistakes
Rotate 3 is not a guarantee of three days of history. Never use forced rotation on production just to test syntax.
Practice
Change the sample policy in a fresh copy to rotate 7; run debug mode and explain why seven versions need not equal seven days.
Related references
Study: logrotate. These are reference names, not deployed links.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Log rotation and retention — Knowledge Check
Apply log, clock and scheduling concepts to private fixtures and VM-only scenarios.
What does rotate 3 control?
What does debug mode do?
Why may a daemon write to an old log after rename?
Which has a separate retention mechanism?