Project

General

Profile

Support #580

Rotating Log Files With Logrotate and Rotatelogs on FreeBSD

Added by Daniel Curtis over 5 years ago. Updated over 5 years ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Logging Server
Target version:
Start date:
03/08/2015
Due date:
% Done:

100%

Estimated time:
0.50 h
Spent time:

Description

This is a simple guide for setting up logrotate and rotatelogs on FreeBSD 9.2-RELEASE.

  • Update the system and ports tree:
    pkg update && pkg upgrade
    portsnap fetch extract
    

Rotating With Logrotate

  • Install logrotate:
    pkg install logrotate
    
  • Edit the logrotate config file:
    vi /usr/local/etc/logrotate.conf
    
    • Add/modify the following:
      /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
      }
      
  • Test logrotate:
    logrotate -d /usr/local/etc/logrotate.conf
    
  • Setup a cronjob to run logrotate:
    crontab -e
    
    • And add the following:
      ## Logrotate at 1 AM in the morning
      0 01 * * * root /usr/local/sbin/logrotate /usr/local/etc/logrotate.conf > /dev/null 2>&1
      
  • Restart cron:
    service cron restart
    

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.
    CustomLog "|bin/rotatelogs /var/logs/logfile 86400" common
    
  • 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.
    CustomLog "|bin/rotatelogs -l /var/logs/logfile.%Y.%m.%d 86400" common
    
  • This configuration will rotate the logfile whenever it reaches a size of 5 megabytes.
    CustomLog "|bin/rotatelogs /var/logs/logfile 5M" common
    
  • 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.
    ErrorLog "|bin/rotatelogs /var/logs/errorlog.%Y-%m-%d-%H_%M_%S 5M" 
    

Resources

History

#1 Updated by Daniel Curtis over 5 years ago

  • Description updated (diff)
  • Status changed from New to In Progress
  • % Done changed from 0 to 50

#2 Updated by Daniel Curtis over 5 years ago

  • Description updated (diff)
  • Status changed from In Progress to Resolved
  • % Done changed from 50 to 100

#3 Updated by Daniel Curtis over 5 years ago

  • Subject changed from Rotating Log Files With Logrotate on FreeBSD to Rotating Log Files With Logrotate and Rotatelogs on FreeBSD
  • Description updated (diff)

#4 Updated by Daniel Curtis over 5 years ago

  • Status changed from Resolved to Closed

Also available in: Atom PDF