Its time we all admitted that rotating our log files is something we should get our act together with and do properly. I was a bit surprised how relevant and simple examples were hard to find online the first time i had to do this, so i figured since i just had to install “logrotate” on my Ubuntu server again today that this is a good day to write about it.
Why is this important? Well if you don’t rotate your logs you will suffer from erosion. A massive problem for servers that don’t get a lot of love and attention…and honestly who wants to love a server? If you servers disk space is filling up with logs, and you are not emptying them faster than they fill up…thats erosion.
The following example tells you how to install logrotate and configure it to rotate your Apache2 log files.
Step 1.
Install logrotate on your server.
sudo apt-get install logrotate
Step 2.
Edit the logrotate config file to tell it where your apache logs are.
sudo vi /etc/logrotate.conf
Add the following to the very bottom of the file:
/var/log/apache2/*.log {
weekly
missingok
rotate 52
compress
delaycompress
notifempty
create 640 root adm
sharedscripts
postrotate
/etc/init.d/apache2 restart > /dev/null
endscript
}
Thats great, but what does it mean?
Line by line, this is the breakdown:
1. /var/log/apache2/*.log
This is the path to the logs you want to rotate. I’ve said *.log here, because i want all logs in this directory to rotate
2. weekly
Tells log rotate to rotate these weekly
3. missingok
Prevents us from getting errors if there are no logs
4. rotate 52
How long should archived files be retained before being deleted
5. compress
duh…
6. delaycompress
Sometimes you dont want to compress the file right away, as the app is writing to the logs and will be for some period of time
7. notifempty
Only rotates logs that are not empy
8. create 640 root adm
This defines the file permissions that the archives will be created with
9. sharedscripts
Ensures any prescript or postscript elements of this log rotation are only run once
10-12. postrotate
This section defines what logrotate should do at the end of the rotation. In this case i’ve told it to restart apache, because we want to make sure new log files get created. Even although log rotate renamed the log file, apache will keep writing to the renamed file until its restarted (this is why we included the option ‘delaycompress’)
13. }
end of the log rotation config

ROTATE YOUR LOGS! SAVE YOUR DISK SPACES! OR I WILL HURT THIS RABBIT!