DEV Community

Cover image for How do you prevent a log file from getting too big with logrotate?
Bobby Iliev
Bobby Iliev

Posted on

How do you prevent a log file from getting too big with logrotate?

Alt Text

Recently a log file on one of my droplets got too big and filled up the disk space of the server, so I decided to share my solution here!

There were two things that I had to do:

  • As the log was really big and was exhausting the available webspace I had to truncate the log. I did this with the following command:
> /var/log/my.log
Enter fullscreen mode Exit fullscreen mode

That way the log is truncated without disrupting the processes.

  • For a more structural solution, it is best to use logrotate. So what I did was to add a new conf file in the /etc/logrotate.d/ directory. The content of the file looked something like this:
/var/log/my.log {
       weekly      
       rotate 4    
       compress    
}                  
Enter fullscreen mode Exit fullscreen mode

A quick rundown of the options:

  • rotate 4 : keep four old log files.
  • weekly : rotate once a week.
  • compress : compress the rotated files, by default gzip is used and results in files ending in .gz.

For more information I would recommend checking the logrotate man page and this tutorial on DigitalOcean.

Hope that this helps!

Top comments (0)