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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7759321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-01 12:29:14  来源:igfitidea点击:

Disable Rails SQL logging in console

sqlruby-on-railsconsole

提问by gylaz

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.

当我在控制台中执行命令时,有没有办法禁用 SQL 查询日志记录?理想情况下,如果我可以禁用它并使用控制台中的命令重新启用它,那就太好了。

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.

我正在尝试调试某些内容并使用“puts”打印出一些相关数据。但是,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 nilyou can set the level of the logger to 1.

nil您可以将记录器的级别设置为 ,而不是将记录器设置为1

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

回答by Ryan Bigg

To turn it off:

要关闭它:

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

To turn it back on:

要重新打开它:

ActiveRecord::Base.logger = old_logger

回答by jrochkind

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

这是一个我认为更简洁的变体,它仍然允许来自 AR 的潜在其他日志记录。在 config/environments/development.rb 中:

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

回答by Christoph Petschnig

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

回答by Micah

For Rails 4 you can put the following in an environment file:

对于 Rails 4,您可以将以下内容放入环境文件中:

# /config/environments/development.rb

config.active_record.logger = nil

回答by fakeleft

In case someone wants to actuallyknock 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 debugin lib/active_record/log_subscriber.rb:50.

写入日志的行(无论如何在 Rails 3.2.16 中)是对debugin的调用lib/active_record/log_subscriber.rb:50

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

回答by Nate Ben

I used this: config.log_level = :infoedit-in config/environments/performance.rb

我用过这个:config.log_level = :info编辑config/environments/performance.rb

Working great for me, rejecting SQL output, and show only rendering and important info.

对我来说很好用,拒绝 SQL 输出,只显示渲染和重要信息。

回答by Telmo Costa

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

回答by Max Williams

Just as an FYI, in Rails 2 you can do

仅供参考,在 Rails 2 中,您可以做到

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

Obviously the curly braces could be replaced with a do endblock if you wanted.

显然,do end如果您愿意,可以用块替换花括号。