How to prevent a log file from being too big using logrotate?

Too big log file and then the server space is running out. Most of the developers have encountered this situation. To avoid this kind of situation we can use logrotate.

In our case, we were using Ubuntu as a server, and logrotate was installed in the server. The logrotate can be installed using the following command:

---------- On Debian and Ubuntu ---------- 
$ aptitude update && aptitude install logrotate 

---------- On CentOS, RHEL and Fedora ---------- 
$ yum update && yum install logrotate

We can then configure the logrotate going inside this directory: /etc/logrotate.d/. For apache2, we can create a file apache2.conf in there and use the following code there:

/var/log/apache2/* {
    weekly
    rotate 3
    size 5M
    compress
}
rotate 3 : keep four old log files.
weekly : rotate once a week.
size 5M : sets the minimum size for the rotation to take place to 5M.
compress : compress the rotated files, by default gzip is used and results in files ending in .gz.

If we need to empty out the big log file, we can use the following command:

$ > /var/log/apache2.log
Reference: