Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the best way to rotate logs for rails application

I have a 1GB slice from slicehost and I have 4 projects running on that box. All 4 applications are ruby on rails application. I was wondering what is the best way to ensure that log files are rotated.

I would prefer to have 4 different log files one for each app rather than having one big log file for all 4 applications.

I am running ubuntu.

I am running passenger.

like image 522
Roger Avatar asked Oct 19 '09 17:10

Roger


People also ask

Which command can be used to rotate the logs?

logrotate is designed to ease administration of systems that generate large numbers of log files. It allows automatic rotation, compression, removal, and mailing of log files. Each log file may be handled daily, weekly, monthly, or when it grows too large. Normally, logrotate is run as a daily cron job.

What utility can you use to automate the rotation of logs?

The logrotate utility makes log rotation fairly easy and automatic. It puts a lot of intelligent practices to use, but to manage and modify how this process works, you would need to be able to peer into the files that control how log files are rotated.

Why should log files be rotated?

The main purpose of log rotation is to restrict the volume of the log data to avoid overflowing the record store, while keeping the log files small enough so viewers can still open them.

How often should logs be rotated?

Each file should be rotated weekly. The log rotation job runs nightly, though, so this can be changed to daily for a specific log file if desired. The three commands that specify how often rotation should take place are daily, weekly and monthly. Keep four sets of log files.


2 Answers

I also use logrotate (you'll have to install via apt-get). Create a new logrotate file in your /etc/logrotate.d/ directory. Here's an example of one of mine:

# for the rails logs
/home/apps/*/shared/log/*log {
  daily
  rotate 14
  notifempty
  missingok
  compress
  sharedscripts
  postrotate
    /usr/bin/touch /home/apps/application1/current/tmp/restart.txt
    /usr/bin/touch /home/apps/application2/current/tmp/restart.txt
  endscript
}
# for the apache logs
/home/apps/logs/*log {
  daily
  rotate 14
  notifempty
  missingok
  compress
  sharedscripts
  postrotate
    /etc/init.d/apache2 restart
  endscript
}

This rotates both rails production.log logs and the apache access/error logs (I run my apps under passenger).

like image 72
Bill Turner Avatar answered Sep 18 '22 19:09

Bill Turner


I'd just use the built-in rotation offered by the rails logger:

# in config/application.rb
config.logger = Logger.new(Rails.root.join('log', "#{Rails.env}.log"), 3, 10.megabytes)

This will rotate the log files once they reach 10MB and save the 3 most recent rotated logs.

like image 37
Gabe Kopley Avatar answered Sep 17 '22 19:09

Gabe Kopley