Log summaries and maintenance
Log reporting converts a stream of records into useful operational information. Separate reporting from retention and rotation. A summary should expose counts and fixed labels, not raw messages that may contain private data. This lesson uses invented records whose first field is INFO, WARN or ERROR; other labels are counted as unknown. Real journal and application formats require appropriate parsers and access controls.
Reports, rotation and retention
Rotation changes which file receives new messages; retention decides how long old logs are kept. Renaming an active file does not automatically redirect a writer that still holds an open descriptor. A service may need its documented reopen mechanism. Copy-and-truncate rotation has a loss window between copying and truncation; avoid homemade rotation for active production logs. Consult logrotate and the Linux service administration track; all changes to actual logs or daemon configuration belong in an authorized disposable VM.
Choose a time window, define units and expected severity labels, and keep summaries deterministic. Use fixed-string or field-based matching rather than searching arbitrary message text for severity. Redaction by allowlisting output fields is safer than claiming a regex can remove all secrets. Treat unrecognized records as a format or data-quality problem. Do not print raw payloads or inspect personal logs in these labs. The first count example reports only three recognized severities; the second also counts all other records, including blank lines, as unknown. Check the fixture write and every rotation step.
Practical examples
Run each example as a separate script in a fresh noninteractive Bash 5.2 process and empty practice directory. Tests use C locale; do not enter expected output as code.
Count severity without exposing messages
printf '%s\n' 'INFO started' 'WARN slow' 'ERROR failed' 'INFO mentions ERROR' > log.fixture || exit 1awk '$1=="INFO" {i++} $1=="WARN" {w++} $1=="ERROR" {e++} END {printf "INFO=%d\nWARN=%d\nERROR=%d\n",i,w,e}' log.fixtureExpected stdout:
INFO=2WARN=1ERROR=1Fixed fields determine severity, not message words.
Count unknown records separately
printf '%s\n' 'INFO DEMO_TOKEN' 'OTHER example' > log.fixture || exit 1awk '$1=="INFO" || $1=="WARN" || $1=="ERROR" {known++; next} {unknown++} END {printf "known=%d unknown=%d\n",known,unknown}' log.fixtureExpected stdout:
known=1 unknown=1Only aggregate counts leave the parser. The synthetic token is not included in output.
Demonstrate an open writer after rename
exec 3> demo.log || exit 1printf 'first\n' >&3 || exit 1mv -- demo.log demo.log.1 || exit 1printf 'second\n' >&3 || exit 1exec 3>&- || exit 1printf 'new\n' > demo.log || exit 1printf 'old:\n' && cat demo.log.1 || exit 1printf 'current:\n'; cat demo.logExpected stdout:
old:firstsecondcurrent:newOn the tested filesystem the descriptor still refers to the renamed file. This is not a production rotation recipe.
Common mistakes
Counting ERROR anywhere in a line also counts messages mentioning that word. Renaming alone cannot guarantee rotation of active writers. Never delete real logs as a lesson exercise.
Practice
Add a synthetic unknown severity and a message containing ERROR with INFO severity. Verify only the first field affects counts. Write a retention policy, not a deletion command.
Next: Backup and restore verification