One of the things I dislike the most is when my VPS (Virtual Private Server) is starting to slow down or is completely full because of large log files. Have you ever moseyed over to the log folder (e.g. /var/log), opened it up, and thought to yourself, "Why do I have 200 MB of logs? What are they all there for? That seems excessive." I have too. That's why I use Logrotate. It helps keep my VPS clean without having to put in any extra work on my part.
/etc/logrotate.d/
Here is a basic example:
“/var/log/myapp/*.log {
daily
rotate 7
compress
missingok
notifempty
} “
• rotate 7 - keep 7 previous logs
• b save disk space
• missingok - which ignores if the log does not exist
• notifempty - do not rotate empty log
Straightforward, yes?
If you do not compress, logs take up too much space. I once forgot to, and my disk filled up in a hurry.
2. Frequently Rotate Rapidly Growing Logs
Some applications create logs continuously throughout the day. For any of those you may have, your logs can be rotated hourly. This keeps your VPS from getting overrun by logs.
3. Test Logrotate Before You Use It
You can preview your logs and setup with:
logrotate -d /etc/logrotate.conf
This will show you exactly what Logrotate will do without making any changes.
If your VPS storage fills up quickly, try Logrotate today and you will see the difference.
Why Big Logs Are Bad
Apps are constantly writing logs to your VPS. Whenever these logs become too large, it can be a problem because:- They take up a lot of disk space.
- They can slow down the VPS.
- They make it hard to run commands, such as opening the logs and reading through them.
What Logrotate Does
Logrotate is a very simple tool for managing log files. It:- Rotates logs (creates a new log and moves/renames old ones).
- Compresses old logs.
- Automatically deletes very old logs.
- Runs daily or weekly automatically.
How to Use Logrotate
To start utilizing logrotate to manage a log file, create a file in the following directory:/etc/logrotate.d/
Here is a basic example:
“/var/log/myapp/*.log {
daily
rotate 7
compress
missingok
notifempty
} “
Explanation of What Each Option Means
• daily - rotate logs daily• rotate 7 - keep 7 previous logs
• b save disk space
• missingok - which ignores if the log does not exist
• notifempty - do not rotate empty log
Straightforward, yes?
Tips I Learned the Hard Way
1. Always compress logsIf you do not compress, logs take up too much space. I once forgot to, and my disk filled up in a hurry.
2. Frequently Rotate Rapidly Growing Logs
Some applications create logs continuously throughout the day. For any of those you may have, your logs can be rotated hourly. This keeps your VPS from getting overrun by logs.
3. Test Logrotate Before You Use It
You can preview your logs and setup with:
logrotate -d /etc/logrotate.conf
This will show you exactly what Logrotate will do without making any changes.
Closing Thoughts
Log files can grow very quickly and become problematic. Logrotate will keep your VPS clean, fast and safe without you having to do much work. I use it on all my servers because it works.If your VPS storage fills up quickly, try Logrotate today and you will see the difference.