Disable Rails SQL logging in console

Is there a way to disable SQL query logging when I'm executing commands in the console? Ideally, it would be great if I can just disable it and re-enable it with a command in the console.

I'm trying to debug something and using "puts" to print out some relevant data. However, the sql query output is making it hard to read.


Edit: I found another solution, since setting the logger to nil sometimes raised an error, if something other than my code tried to call logger.warn

Instead of setting the logger to nil you can set the level of the logger to 1 .

ActiveRecord::Base.logger.level = 1 # or Logger::INFO

To turn it off:

old_logger = ActiveRecord::Base.logger
ActiveRecord::Base.logger = nil

To turn it back on:

ActiveRecord::Base.logger = old_logger

Here's a variation I consider somewhat cleaner, that still allows potential other logging from AR. In config/environments/development.rb :

config.after_initialize do
  ActiveRecord::Base.logger = Rails.logger.clone
  ActiveRecord::Base.logger.level = Logger::INFO
end

这可能不是适用于控制台的解决方案,但Rails有一个解决此问题的方法:记录器#沉默

ActiveRecord::Base.logger.silence do
  # the stuff you want to be silenced
end
链接地址: http://www.djcxy.com/p/63558.html

上一篇: 基础架构管理和Rails 3.1+应用程序的部署

下一篇: 在控制台中禁用Rails SQL日志记录