Support #580
Updated by Daniel Curtis almost 10 years ago
{{>toc}} This is a simple guide for setting up logrotate and rotatelogs on FreeBSD 9.2-RELEASE. * Update the system and ports tree: <pre> pkg update && pkg upgrade portsnap fetch extract </pre> h2. Rotating With Logrotate * Install logrotate: <pre> pkg install logrotate </pre> * Edit the logrotate config file: <pre> vi /usr/local/etc/logrotate.conf </pre> #* Add/modify the following: <pre> /var/log/httpd-access_log { daily size 1024k rotate 30 copytruncate compress notifempty missingok } /var/log/httpd-error_log { daily size 1024k rotate 30 copytruncate compress notifempty missingok } </pre> * Test logrotate: <pre> logrotate -d /usr/local/etc/logrotate.conf </pre> * Setup a cronjob to run logrotate: <pre> crontab -e </pre> #* And add the following: <pre> ## Logrotate at 1 AM in the morning 0 01 * * * root /usr/local/sbin/logrotate /usr/local/etc/logrotate.conf > /dev/null 2>&1 </pre> * Restart cron: <pre> service cron restart </pre> h2. Rotating With Rotatelogs Rotate logs is a tool provided by Apache for use in conjunction with Apache's piped logfile feature. It supports rotation based on a time interval or maximum size of the log. * This creates the files /var/logs/logfile.nnnn where nnnn is the system time at which the log nominally starts (this time will always be a multiple of the rotation time, so you can synchronize cron scripts with it). At the end of each rotation time (here after 24 hours) a new log is started. <pre> CustomLog "|bin/rotatelogs /var/logs/logfile 86400" common </pre> * This creates the files /var/logs/logfile.yyyy.mm.dd where yyyy is the year, mm is the month, and dd is the day of the month. Logging will switch to a new file every day at midnight, local time. <pre> CustomLog "|bin/rotatelogs -l /var/logs/logfile.%Y.%m.%d 86400" common </pre> * This configuration will rotate the logfile whenever it reaches a size of 5 megabytes. <pre> CustomLog "|bin/rotatelogs /var/logs/logfile 5M" common </pre> * This configuration will rotate the error logfile whenever it reaches a size of 5 megabytes, and the suffix to the logfile name will be created of the form errorlog.YYYY-mm-dd-HH_MM_SS. <pre> ErrorLog "|bin/rotatelogs /var/logs/errorlog.%Y-%m-%d-%H_%M_%S 5M" </pre> h2. Resources * https://www.freebsd.org/cgi/man.cgi?query=logrotate&manpath=SuSE+Linux/i386+11.3 * https://teklimbu.wordpress.com/2007/10/16/managing-your-linuxunix-log-files-using-logrotate/ * http://www.thegeekstuff.com/2010/07/logrotate-examples/ * http://httpd.apache.org/docs/2.2/programs/rotatelogs.html