Sub topic 4
Why Logs Must Be Rotated
Assume you are comfortable with the Linux command line, sudo, systemd basics, and the /var/log directory. This chapter explains why log files need a lifecycle instead of growing forever.
The problem with growing logs
A log records events such as service starts, failed login attempts, web requests, and application errors. A busy service can write thousands of lines each hour. If the service always appends to the same file, that file can eventually consume the filesystem.
A full filesystem can stop more than logging. Databases may fail to write, package updates may stop, and services may become unhealthy. Logging the failure may also fail because there is no space left.
Large files create other problems:
- Searching and copying them takes longer.
- Backups use more storage and bandwidth.
- A single damaged file can contain a long history of unrelated events.
- Retaining every event forever may create unnecessary privacy and security exposure.
Log rotation divides one active log into smaller historical files. A rotation policy usually renames the current file, creates a fresh file, compresses older files, and deletes files past their retention limit.
# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily
rotate 14
compress
delaycompress
missingok
notifempty
create 0640 myapp adm
sharedscripts
postrotate
/usr/bin/systemctl kill -s HUP myapp.service
endscript
}This policy rotates the files daily and keeps 14 generations. compress saves space by compressing old files. delaycompress leaves the newest rotated file uncompressed for one cycle, which can help programs that still inspect it. missingok avoids an error when a file does not exist. notifempty skips empty files.
The create directive creates the replacement file with mode 0640, owner myapp, and group adm. File mode 0640 gives the owner read and write access, the group read access, and everyone else no access.
Why applications must reopen files
Renaming a file does not automatically make a running process use the new pathname. A process may still have the old file open through a file descriptor, which is the kernel's reference to an open file.
After rotation, the service should close and reopen its log. The postrotate command sends the HUP signal to myapp.service. The application must support this signal or another documented reload method.
If it does not reopen the file, logging may continue into the renamed file. Disk usage can then grow even though a new file appears in the directory.
Schedule versus size
Rotation can run on a schedule such as daily or weekly. Some policies also rotate when a file exceeds a size limit. A schedule is predictable, while a size limit protects against a sudden burst of messages.
Rotation is separate from the journal's retention settings. logrotate manages selected files, while journald manages its own journal data. Configure both when a server uses both storage systems.
Exercise
A service continues writing to app.log.1 after rotation. What part of the policy should you inspect first?
Show answer
Inspect the postrotate action and confirm that the service received a supported reload or reopen signal.