开发者

Way to disable Rails SQL logs?

开发者 https://www.devze.com 2022-12-09 10:33 出处:网络
Is there a way to disable SQL logs in Rails, other than changing the log level? I\'ve got some logger.debug statements that I\'d like to print out in my ActiveRecord models, but I want to hide the SQL

Is there a way to disable SQL logs in Rails, other than changing the log level? I've got some logger.debug statements that I'd like to print out in my ActiveRecord models, but I want to hide the SQL statemen开发者_StackOverflow中文版ts.


You can moneky-patch it, put this in a file such as config/initializers/disable_ar_logging.rb:

class ActiveRecord::ConnectionAdapters::AbstractAdapter
  def log_info(*args); end
end


Dan,

Is this in production or development mode? If it's development mode this is usually what I do:

logger.info("DEBUG my message here")
logger.info("DEBUG #{my_object.inspect}")

tail -f log/development | grep DEBUG


Here's what worked for me in Rails 3.0.5:

  class ActiveRecord::ConnectionAdapters::AbstractAdapter
    def log(sql, name)
      name ||= "SQL"
      yield
    rescue Exception => e
      message = "#{e.class.name}: #{e.message}: #{sql}"
      @logger.debug message if @logger
      raise translate_exception(e, message)
    end
  end

It's this method with the line that writes to the log removed. SQL cache hits are still displayed in the log and I haven't figured out how to disable those.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号