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

发布于 2024-12-10 08:39:50 字数 363 浏览 0 评论 0原文

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

我正在尝试调试某些内容并使用“puts”打印出一些相关数据。然而,sql 查询输出使其难以阅读。


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

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

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(10

很酷不放纵 2024-12-17 08:39:50

关闭它:

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

重新打开它:

ActiveRecord::Base.logger = old_logger

To turn it off:

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

To turn it back on:

ActiveRecord::Base.logger = old_logger
缘字诀 2024-12-17 08:39:50

这可能不是适合控制台的解决方案,但 Rails 有一个解决此问题的方法:Logger#silence

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

This might not be a suitable solution for the console, but Rails has a method for this problem: Logger#silence

ActiveRecord::Base.logger.silence do
  # the stuff you want to be silenced
end
妄司 2024-12-17 08:39:50

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

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

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
猫烠⑼条掵仅有一顆心 2024-12-17 08:39:50

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

# /config/environments/development.rb

config.active_record.logger = nil

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

# /config/environments/development.rb

config.active_record.logger = nil
無心 2024-12-17 08:39:50

如果有人想要实际上删除 SQL 语句日志记录(不更改日志记录级别,同时保留 AR 模型的日志记录):

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

该调试方法由 ActiveSupport::LogSubscriber 定义。

因此,我们可以通过覆盖日志来消除日志记录,如下所示:

module ActiveSupport
  class LogSubscriber
    def debug(*args, &block)
    end
  end
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):

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.

That debug method is defined by 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
且行且努力 2024-12-17 08:39:50

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

非常适合我,拒绝 SQL 输出,并且仅显示渲染和重要信息。

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

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

叹梦 2024-12-17 08:39:50

我使用 activerecord 6.0.3.3,并且必须包含 ActiveSupport::LoggerSilence

include ActiveSupport::LoggerSilence

ActiveSupport::LoggerSilence.silence do
    ## everything you want to silence
end

然而,这不适用于与创建或删除 SQL 表(例如 ActiveRecord::Migration.drop_table)相关的任何内容。为了让这个问题消失,我补充道:

ActiveRecord::Schema.verbose = false

I use activerecord 6.0.3.3 and I had to include ActiveSupport::LoggerSilence

include ActiveSupport::LoggerSilence

ActiveSupport::LoggerSilence.silence do
    ## everything you want to silence
end

This however did not work with anything related to creating or deleting SQL tables like ActiveRecord::Migration.drop_table. For this to be silenced I added:

ActiveRecord::Schema.verbose = false
噩梦成真你也成魔 2024-12-17 08:39:50

在 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

In Rails 3.2 I'm doing something like this in 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
够运 2024-12-17 08:39:50

仅供参考,在 Rails 2 中,您可以执行以下操作:

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

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

Just as an FYI, in Rails 2 you can do

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.

一个人练习一个人 2024-12-17 08:39:50

我必须为 ActiveRecord 6 解决这个问题,并且我的答案基于 fakeleft 的响应,但它不太正确,因为它抑制了其他日志记录,例如嵌套视图的日志记录。我所做的是创建 config/initializers/activerecord_logger.rb

# Suppress SQL statement logging if necessary
# This is a dirty, dirty trick, but it works:
if ENV["ACTIVERECORD_HIDE_SQL"].present?
  module ActiveRecord
    class LogSubscriber
      def sql(event)
      end
    end
  end
end

AR 6 中的日志订阅者有一个我们想要隐藏的 sql 事件,因此它的目标非常狭窄跳过那个事件。

I had to solve this for ActiveRecord 6, and I based my answer on fakeleft's response, but it wasn't quite right, since it was suppressing other logging such as the logging of nested views. What I did was created config/initializers/activerecord_logger.rb:

# Suppress SQL statement logging if necessary
# This is a dirty, dirty trick, but it works:
if ENV["ACTIVERECORD_HIDE_SQL"].present?
  module ActiveRecord
    class LogSubscriber
      def sql(event)
      end
    end
  end
end

The log subscriber in AR 6 has a sql event that we want to hide, so this is very narrowly targeted to skip that event.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文