Logrotate Configuration for Applications
Logrotate config for application logs with daily rotation, compression, and post-rotate hooks.
# /etc/logrotate.d/webapp
/var/log/webapp/*.log {
daily
missingok
rotate 14
compress
delaycompress
notifempty
create 0640 webapp webapp
sharedscripts
postrotate
systemctl reload webapp 2>/dev/null || true
endscript
}
# /etc/logrotate.d/nginx
/var/log/nginx/*.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
create 0640 www-data adm
sharedscripts
postrotate
[ -f /var/run/nginx.pid ] && kill -USR1 $(cat /var/run/nginx.pid)
endscript
}
# Test configuration
# sudo logrotate -d /etc/logrotate.d/webapp
# Force rotation
# sudo logrotate -f /etc/logrotate.d/webappUse Cases
- Preventing disk space exhaustion from logs
- Maintaining log history with compression
- Graceful log rotation for running services
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
Systemd Service File for Node.js
Systemd unit file to run a Node.js app as a managed service with auto-restart and logging.
Linux Cron Job Setup Examples
Common crontab entries for database backups, log cleanup, health checks, and certificate renewal.
Audit Log Trigger Function
Automatically record all INSERT, UPDATE, and DELETE operations into an audit log table via PostgreSQL triggers.
Structured Logging with structlog
Configure structlog for JSON-formatted structured logging with request context, timestamps, and log levels.