— Linux, MLOps, CS, BE — 1 min read
Log rotation is essential for managing log files in production systems, ensuring that logs do not consume excessive disk space while maintaining accessibility for troubleshooting and analytics. Recently, I configured log rotation for our microservice system. Below are my detailed notes and configurations, which can serve as a guide for implementing log rotation in your environment.
/etc/logrotate.d/realtime_logs1/path/to/realtime-service/logs/*.log {2 su root root3 size 10M4 rotate 99999995 missingok6 notifempty7 copytruncate8}size 10M: Rotate logs when they reach 10 MB.rotate 9999999: Retain a very high number of old logs (effectively unlimited).missingok: Skip missing files without generating errors.notifempty: Avoid rotating empty logs.copytruncate: Truncate the log file after rotation./etc/logrotate.d/nginx1/var/log/nginx/*.log {2 daily3 rotate 104 missingok5 notifempty6 compress7 delaycompress8 create 0640 nginx root9 sharedscripts10 postrotate11 if [ -f /run/nginx.pid ]; then12 kill -USR1 `cat /run/nginx.pid`13 fi14 endscript15}compress: Compress old logs to save space.postrotate: Reload NGINX to ensure it uses new log files.To automate log rotation, the following cron jobs were added to rotate logs every hour:
10 * * * * /usr/sbin/logrotate /etc/logrotate.d/realtime_logsFor daily rotation, you can modify the schedule accordingly.
rotate to specify the number of old logs to keep.missingok: Skip missing files without errors.notifempty: Skip empty logs.