Did you ever have to check a log file and when tried to open, it was huge?
I have, depressing.
So what? Logrotate it!
From package description:
“logrotate is designed to ease administration of systems that generate
large numbers of log files. It allows automatic rotation, compression,
removal, and mailing of log files. Each log file may be handled daily,
weekly, monthly, or when it grows too large.”
How to handle logs with logrotate?
The sample code here was used on Ubuntu 16.04, but I’m sure you will figure out how to make it work on your system.
Step 1: Install it, if not installed yet
Ubuntu 16.04 already have Logrotate installed, but anyway.
sudo apt-get update
sudo apt-get install logrotate
Step 2: Add you app specific file
Specific files are located on:
/etc/logrotate.d/
Create a file like:
/etc/logrotate.d/[your_app_name]
With a content like this:
/path/to/your/log/file.log {
daily
missingok
rotate 30
compress
delaycompress
notifempty
copytruncate
}
The options means:
- daily – do it every day
- missingok – if no log, no problem, no error
- rotate 30 – keep 30 old files, or in our case, keep 30 days of log
- compress – compress the old files
- delaycompress – delay the file compressing action, nice to avoid write concurrency in the log file
- notifempty – if no content on log file, do nothing
- copytruncate – truncate the original log file in place after creating a copy, instead of moving the old log file and optionally creating a new one
You can check other options for Ubuntu package on the link below:
http://manpages.ubuntu.com/manpages/xenial/man8/logrotate.8.html
Step 3: Wait until next day
On Ubuntu 16.04, no further actions are need because there is a cron job already running the logrotate daily, so your new file will be loaded on next day.
To confirm if the cron job is there, check the file:
/etc/cron.daily/logrotate
Check Logrotate status
You can check the logrotate status, remember, your new file should appear on the next day.
cat /var/lib/logrotate/status
One config, several files
Here is another example of file that rotate all log files inside a specific directory, often used by apps deployed with mina gem, but you can use the same concept in your environment.