8

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.

ylluminate
  • 12,102
  • 17
  • 78
  • 152
  • Running on Mac OS X for `development`. Some are on linux in the group. If I can use a non-specific method that would be best, or I can do something with `case CONFIG['host_os']` if necessary. – ylluminate Mar 06 '12 at 10:10
  • 1
    http://stackoverflow.com/questions/1036821/how-to-delete-rails-log-file-after-certain-size – Kuba Mar 06 '12 at 10:10
  • I just clarified it a little. My real goal here is to KEEP the tail of the file, or the MOST RECENT log entries, discarding the oldest ones. – ylluminate Mar 06 '12 at 10:21
  • What are your thoughts on the above possible solution or temporary work around? – ylluminate Mar 06 '12 at 11:25
  • production question: http://stackoverflow.com/questions/4883891/ruby-on-rails-production-log-rotation – Ciro Santilli OurBigBook.com Sep 22 '14 at 13:47

2 Answers2

11

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.

Tomas Markauskas
  • 11,496
  • 2
  • 33
  • 35
  • 2
    This works nicely, however in 3.2 the Logger class is deprecated in favor of ActiveSupport::BufferedLogger, which apparently does not support rotation. Frustrating! – George Armhold Aug 10 '12 at 15:21
  • @CaffeineComa can you support your inference? I'm using Rails 3.2.2 and this works pretty well, at least in development mode, that is the scope of this question. – Claudio Floreani Apr 06 '13 at 09:33
1

On OSX i would use newsyslog

/etc/newsyslog.conf

On a Linux OS: logrotate

logrotate

Roger
  • 7,535
  • 5
  • 41
  • 63
  • Any thoughts on using `tail -c $[100*1024*1024] development.log`? – ylluminate Mar 06 '12 at 10:35
  • well tbh not really, its pretty massive. I normally (for debugging / devlopment) clear my console (cmd-k) and then run my code to see what happened. Watching a 100 MB trace, no thank you :) And it depends as well if other people are running on your dev env. Since then you need to split the clutter of the different IP's. – Roger Mar 06 '12 at 10:45
  • Hahahah, yeah, that's still a bit large. Once we don't have such crazy fire hose spew going on for some data I'll trim it down to something more like 10MB probably. – ylluminate Mar 06 '12 at 11:26