Search code examples
ruby-on-railsruby-on-rails-3rake

How do I disable logging for a rake task?


I have a rake task that gets run as a cron job once a day. It performs some maintenance on a model that currently has 57k+ records in it. This adds hundreds of thousands of lines to the log each time the task is run for whichever environment it's currently in. (Currently it's just in development.)

How can I disable logging for a specific rake task or group of tasks, but leave logging alone for the rest of the app and leave it alone for the model/methods that are called from the task?


Solution

  • The Rails logger is an ActiveSupport::BufferedLogger object, but can be re-assigned to any other kind of Logger object (or any object that responds to the Logger methods) that you wish, including something like this:

    dev_null = Logger.new("/dev/null")
    Rails.logger = dev_null
    ActiveRecord::Base.logger = dev_null
    

    Put these lines at the top of your Rake task and all logging output will be inexpensively sent to /dev/null, which is best explained by its Wikipedia article.