rake task logger" />

Logging with Rake

July 8, 2009

I’ve gotten a little tired of failing rake tasks that don’t give me enough diagnostic information with just the –trace option. Here’s a little snippet to redirect the ActiveRecord (and SQL in debug mode) output to stdout. Then pipe it into a file for safe keeping.

rake mytask LOG=DEBUG > mytask.out

def set_logger
  #configure ActiveRecord to log to Stdout
  if (log = (ENV['LOG']||ENV['log'])) && log.to_s.downcase != 'false'
    ActiveRecord::Base.logger = Logger.new(STDOUT)
    ActiveRecord::Base.logger.level = begin
      log =~ /^\d*$/ ? log.to_i : Logger.const_get(log.upcase)
    rescue
      Logger::INFO
    end
    puts "ActiveRecord to STDOUT level #{ActiveRecord::Base.logger.level}"
    ActiveRecord::Base.connection.instance_variable_set('@logger', ActiveRecord::Base.logger)
    Paperclip.options[:log] = true
  end
end

Leave a Reply