logrotate

tl;dr

If you are developing some application that writes a lot of logs use `logrotate` to keep them short and don’t allow them to reach gigabytes.


I’m developing in Symfony framework on daily basis. And on the development environment, it creates lots of logs. Every request is logged, every command is logged, every event subscriber is logged. And if you’re using some older version deprecations are also logged. On my computer, every refresh in browser creates over 1 MB of logs! My friend gets almost 10 MB…

Because I reinstalled my computer lately and want to keep it as clean as possible I was searching for some way to prevent logs to expand to GBs of data. I’ve never used logs from a month, a week or a day ago. The most useful ones are from a present, so it’ll be not a loss to cleaning them every day.

I wanted to use the cron job to delete log files every day. But there are some problems: my computer is not always on, so running cron once a day might not be suitable. And it appears to me as a pretty primitive solution. So I googled…

logrotate to the rescue

And I googled `logrotate`, simple tool to managing log files. I a few words it uses some configuration for specific log files (or even directories) and keeps as many versions as you want, each might be daily, weekly or monthly version.

For me the configuration for Symfony logs looks like this:

~/Sites/SymfonyApp/app/logs/*.log {
    daily
    size 50M
    rotate 1
    notifempty
    missingok
    nocompress
}

This configuration means, that I want to make every `*.log` file in directory `~/Sites/SymfonyApp/app/logs/` a daily log, that should be created if it reaches at least 50 MB in size, and I want to keep only one old version at a time. I’m using OS X and homebrew package manager so I placed this configuration in `/usr/local/etc/logrotate.d/symfony_app` file and logrotate loads any file present it this directory automatically, so you can easily manage your configurations. I have one for Nginx and MariaDB as well:

/usr/local/var/log/nginx/*.log {
    daily
    size 50M
    rotate 3
    notifempty
    missingok
    nocompress
}

MariaDB config is very similar.

I used tips and settings from this site ? https://support.rackspace.com/how-to/understanding-logrotate-utility/, `logrotate` is clearly explained there.

You can run logrotate anytime you want calling `/usr/local/sbin/logrotate -f /usr/local/etc/logrotate.conf` but running it manually is primitive, right?

logrotate automation

So I used… cron ? Daily one will be not useful because I don’t know what hour should I run it, so I created one running every 2 hours. This is a good compromise, it runs not too often but it surely runs at least once a day. Creating cron is very easy (assuming you have one installed):
1. edit your crontab `crontab -e`
2. create entry `0 */2 * * * /usr/local/sbin/logrotate /usr/local/etc/logrotate.conf`
3. save it
If you’re not familiar with cron syntax this one `0 */2 * * *` means, that I want to run given the command at minute 0 every 2 hours, every day, every month and every weekday. This is nice tool that explains the syntax pretty good ? https://crontab.guru/#0_*/2_*_*_*

Summary

And that’s all. Nice tool resolving aproblem of big log files and running automatically in the background.

This is how the Symfony log directory looks like today
symfony logs
`dev.log` was rotate today and `test.log` was rotated almost 2 weeks ago and new rotation will occur when it reaches 50M. This is just awesome ?

Want more #GetNoticed2017 posts?