I've written a Rake helper to silence both. You can adapt it for other uses, but I think it's mostly relevant in Rake tasks.
# lib/rake_helpers.rb
module Rake
module Helpers
# Silence the Rails logger as well as the SQL logger
# Call with a block e.g
# silent { my block code }
def silent
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
alias_method :original_log_info, :log_info
def silent_log_info(*args); end
alias_method :log_info, :silent_log_info
end
@@old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = Class.new { def method_missing(*args); end; }.new
yield
ActiveRecord::Base.logger = @@old_logger
ActiveRecord::ConnectionAdapters::AbstractAdapter.class_eval do
alias_method :log_info, :original_log_info
end
end
end
end
And sample usage:
# lib/tasks/db.rake
require 'rake_helpers'
include Rake::Helpers
namespace :db do
namespace :update do
desc "Update counter caches"
task :counters => :environment do
silent do
ActiveRecord::Base.transaction do
[Artist, Album].each do |model|
model.find_in_batches(:batch_size => 5000) do |batch|
batch.each { |record| record.update_counts }
end
end
end
end
end
end
end

1 comment:
You could use logger.silence: http://apidock.com/rails/Logger/silence
Post a Comment