This is a very short simple entry but it may help you. As many others you may be running a web server, or any other service that creates some logging information. Enable log rotation on FreeBSD will keep those log files in a manageable size (at least more readable) and they won’t grow and expand eternally in your /var/log directory.
For this case we’ll see how to rotate logs for the Apache web server but you may adapt this information to your particular needs. To enable log rotation on FreeBSD there is a very useful utility called newsyslog. In order to get a deeper knowledge on how to set the tool and the configuration requirements you may take a look at this other man page.
As you may already know configuration files for the FreeBSD operating system are located in the /etc directory. As a reminder, application configuration files are not set at the same path but rather on /usr/local/etc. For the sake of clarity FreeBSD does differentiate base system configuration files from third party applications such as Apache, MySQL and the rest of the other more than twenty four thousand ports available to this date.
In order to enable log rotation on FreeBSD we will edit the /etc/newsyslog.conf file. You may encounter entries in the file similar to these:
# logfilename [owner:group] mode count size when flags [/pid_file] [sig_num]
/var/log/all.log 600 7 * @T00 J
/var/log/amd.log 644 7 100 * J
/var/log/auth.log 600 7 100 @0101T JC
/var/log/console.log 600 5 100 * J
/var/log/cron 600 3 100 * JC
/var/log/daily.log 640 7 * @T00 JN
/var/log/debug.log 600 7 100 * JC
/var/log/init.log 644 3 100 * J
/var/log/kerberos.log 600 7 100 * J
/var/log/lpd-errs 644 7 100 * JC
/var/log/maillog 640 7 * @T00 JC
/var/log/messages 644 5 100 @0101T JC
/var/log/monthly.log 640 12 * $M1D0 JN
And you have to add the following lines to successfully rotate your Apache logs in FreeBSD.
/var/log/httpd-access.log www:www 644 9 * $W1D4 J /var/run/httpd.pid 30
/var/log/httpd-error.log www:www 644 9 * $W1D4 J /var/run/httpd.pid 30
What does all this mean?
/var/log/httpd-access.log – You first set the file name you want to rotate.
www:www – Afterwards you give the user and group owners of the file in question.
644 – Then you set the right permissions. You can set them at your own discretion, so for example you can set them at 440 so they become read only for the www user and group and no one else.
9 – By setting a number in the next column you set the number of files that will be kept excluding the current one. Therefore you will have the last 10 log files. The rest will be discarded. Depending on the time frame you set this to happen you will have more or less days or weeks of files available for your inspection if needed.
* – the wild card here states not to rotate the files based on their size.
$W1D4 – This sets the time frame rotation. This simply tells the system to rotate the above mentioned file to be rotated every Monday at 04.00 am.
The “J” option tells the program to archive the logs with bzip2 compression.
/var/run/httpd.pid – This states the process ID, in this case for the httpd server.
30 – This sends a signal to gracefully restart the httpd server.
If you enable log rotation on FreeBSD you are not only following good practice guidelines, you will also have a cleaner and clearer environment to work on. And that is always something nice to have.