Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Log rotation in Rails DEVELOPMENT mode?

For a Rails 3.1 (will be 3.2 very soon), I have exceptionally verbose logs that have a lot of additional worker information spewing forth into them.

I routinely end up with multigigabyte development.log files. I've seen some various chatter around about rotating production logs, however I've not found anything that seems applicable to development.log rotation.

How do you rotate your development.log at every 100.megabytes or so? OR WHAT I WOULD PREFER is to actually truncate the head of the file so that only the most recent items remain in the log, up to 100MB of the most recent entries.

I have played with this a little and am thinking more and more than nothing quite like this exists at present and that perhaps I should implement something that will use the ruby File.truncate somehow, however I'm not sure of the efficacy of this yet on the tail end of the file as of yet.

like image 293
ylluminate Avatar asked Mar 06 '12 09:03

ylluminate


People also ask

How often logs should 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.

What is the purpose of log rotation?

In information technology, log rotation is an automated process used in system administration in which log files are compressed, moved (archived), renamed or deleted once they are too old or too big (there can be other metrics that can apply here).

How do I rotate a syslog?

If you want to rotate /var/log/syslog it needs to be listed in a logrotate config file somewhere, and you just run logrotate . If it rotated recently, then logrotate -f to force it to do it again. So, you need that in a file, normally either /etc/logrotate. conf or as a file snippet in /etc/logrotate.

How do you force a log to rotate in the VAR log directory?

If you want to force Logrotate to rotate the log file when it otherwise would not have, use the --force flag: logrotate /home/sammy/logrotate. conf --state /home/sammy/logrotate-state --verbose --force.


2 Answers

You can actually tell the Ruby Logger class to rotate the files in the constructor:

http://corelib.rubyonrails.org/classes/Logger.html#M000163

Example:

Logger.new(name, shift_age = 7, shift_size = 1048576)

In one of my enrivornment files I have the following line:

config.logger = Logger.new("#{RAILS_ROOT}/log/#{ENV['RAILS_ENV']}.log", 10, 1048576)

This keeps the last 10 logfiles which are rotated every 1 MB.

like image 79
Tomas Markauskas Avatar answered Nov 15 '22 16:11

Tomas Markauskas


On OSX i would use newsyslog

/etc/newsyslog.conf

On a Linux OS: logrotate

logrotate

like image 40
Roger Avatar answered Nov 15 '22 16:11

Roger