简体   繁体   English

在控制台中禁用Rails SQL日志记录

[英]Disable Rails SQL logging in console

Is there a way to disable SQL query logging when I'm executing commands in the console? 当我在控制台中执行命令时,有没有办法禁用SQL查询日志记录? 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. 我正在尝试调试一些东西并使用“puts”打印出一些相关数据。 However, the sql query output is making it hard to read. 但是,sql查询输出使其难以阅读。


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 编辑:我找到了另一个解决方案,因为将记录器设置为nil有时会引发错误,如果我的代码以外的其他东西试图调用logger.warn

Instead of setting the logger to nil you can set the level of the logger to 1 . 您可以将记录器的级别设置为1而不是将记录器设置为nil

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. 这是我认为更清洁的一种变体,它仍允许从AR进行潜在的其他日志记录。 In config/environments/development.rb : 在config / environments / development.rb中:

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

This might not be a suitable solution for the console, but Rails has a method for this problem: Logger#silence 这可能不是适合控制台的解决方案,但Rails有一个解决此问题的方法: Logger#silence

ActiveRecord::Base.logger.silence do
  # the stuff you want to be silenced
end

In case someone wants to actually knock out SQL statement logging (without changing logging level, and while keeping the logging from their AR models): 如果有人想要实际删除SQL语句日志记录(不更改日志记录级别,同时保持其AR模型的日志记录):

The line that writes to the log (in Rails 3.2.16, anyway) is the call to debug in lib/active_record/log_subscriber.rb:50 . 写入日志的行(在Rails 3.2.16中,无论如何)是对lib/active_record/log_subscriber.rb:50 debug的调用。

That debug method is defined by ActiveSupport::LogSubscriber . 该调试方法由ActiveSupport::LogSubscriber定义。

So we can knock out the logging by overwriting it like so: 所以我们可以通过覆盖它来淘汰日志:

module ActiveSupport
  class LogSubscriber
    def debug(*args, &block)
    end
  end
end

For Rails 4 you can put the following in an environment file: 对于Rails 4,您可以将以下内容放在环境文件中:

# /config/environments/development.rb

config.active_record.logger = nil

I used this: config.log_level = :info edit-in config/environments/performance.rb 我使用了这个: config.log_level = :info edit-in config/environments/performance.rb

Working great for me, rejecting SQL output, and show only rendering and important info. 对我很有用,拒绝SQL输出,只显示渲染和重要信息。

In Rails 3.2 I'm doing something like this in config/environment/development.rb: 在Rails 3.2中我在config / environment / development.rb中做了类似的事情:

module MyApp
  class Application < Rails::Application
    console do
      ActiveRecord::Base.logger = Logger.new( Rails.root.join("log", "development.log") )
    end
  end
end

Just as an FYI, in Rails 2 you can do 就像一个FYI,在Rails 2中你可以做到

ActiveRecord::Base.silence { <code you don't want to log goes here> }

Obviously the curly braces could be replaced with a do end block if you wanted. 显然,如果你do end ,花括号可以用do end block替换。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM