Rotate and compress application logs with logrotate
8 steps · about 1 h 58 min · Beginner
- Why logs must be rotated at all (article, Wikipedia, 8 min)
Explains rotation, retention and compression as the standard answer to ever-growing log files.
- Read the logrotate directives (reference, man7.org, 20 min)
The manual defines daily/weekly, rotate, compress, missingok, notifempty and every other directive you will write.
- Understand logrotate.conf and include files (reference, linux.die.net, 10 min)
Shows how the global file and /etc/logrotate.d snippets combine, so your app gets its own policy file.
- Write a policy for your own app's log (tutorial, DigitalOcean, 20 min)
Walks through a new config in /etc/logrotate.d and a dry run with the debug flag.
- Check how rotation is scheduled and debugged (reference, ArchWiki, 10 min)
Covers the logrotate timer, the state file and forcing a rotation to test a change.
- Choose copytruncate or create plus postrotate (tutorial, Better Stack, 20 min)
Compares the two ways to hand a fresh file to a running process and when each loses or keeps lines.
- See how a real server reopens its logs (reference, Apache HTTP Server, 15 min)
Apache's log-rotation and piped-log sections show why a graceful restart or signal is needed after a move.
- Practise with size, dateext and maxage examples (article, The Geek Stuff, 15 min)
Short worked examples of size-based rotation, date suffixes and pruning old archives.