Why Rotate Logs?
Without rotation, log files grow indefinitely until they fill your disk. Logrotate compresses, rotates, and removes old log files automatically.
Default Configuration
The main config is /etc/logrotate.conf. Application-specific configs go in /etc/logrotate.d/.
Creating a Custom Config
Create /etc/logrotate.d/myapp:
/var/log/myapp/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 www-data www-data
sharedscripts
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}Key Directives
daily/weekly/monthly— rotation frequencyrotate 14— keep 14 rotated copiescompress— gzip old logsdelaycompress— wait one rotation before compressingmissingok— don't error if log file is missingnotifempty— don't rotate empty filescreate 0640 user group— create new log file with these permissionspostrotate/endscript— commands to run after rotation
Test Configuration
# Dry run (shows what would happen)
sudo logrotate -d /etc/logrotate.d/myapp
# Force rotation
sudo logrotate -f /etc/logrotate.d/myapp