在控制台中禁用 Rails SQL 日志记录
当我在控制台中执行命令时,有没有办法禁用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
关闭它:
重新打开它:
To turn it off:
To turn it back on:
这可能不是适合控制台的解决方案,但 Rails 有一个解决此问题的方法:Logger#silence
This might not be a suitable solution for the console, but Rails has a method for this problem: Logger#silence
我认为这是一个更干净的变体,仍然允许来自 AR 的潜在其他日志记录。在 config/environments/development.rb 中:
Here's a variation I consider somewhat cleaner, that still allows potential other logging from AR. In config/environments/development.rb :
对于 Rails 4,您可以将以下内容放入环境文件中:
For Rails 4 you can put the following in an environment file:
如果有人想要实际上删除 SQL 语句日志记录(不更改日志记录级别,同时保留 AR 模型的日志记录):
写入日志的行(无论如何,在 Rails 3.2.16 中) ) 是对
lib/active_record/log_subscriber.rb:50
中的debug
的调用。该调试方法由 ActiveSupport::LogSubscriber 定义。
因此,我们可以通过覆盖日志来消除日志记录,如下所示:
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
inlib/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:
我使用了这个:
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.
我使用 activerecord 6.0.3.3,并且必须包含
ActiveSupport::LoggerSilence
然而,这不适用于与创建或删除 SQL 表(例如
ActiveRecord::Migration.drop_table
)相关的任何内容。为了让这个问题消失,我补充道:I use activerecord 6.0.3.3 and I had to include
ActiveSupport::LoggerSilence
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:在 Rails 3.2 中,我在 config/environment/development.rb 中执行类似的操作:
In Rails 3.2 I'm doing something like this in config/environment/development.rb:
仅供参考,在 Rails 2 中,您可以执行以下操作:
显然,如果您愿意,可以用
do end
块替换大括号。Just as an FYI, in Rails 2 you can do
Obviously the curly braces could be replaced with a
do end
block if you wanted.我必须为 ActiveRecord 6 解决这个问题,并且我的答案基于
fakeleft
的响应,但它不太正确,因为它抑制了其他日志记录,例如嵌套视图的日志记录。我所做的是创建config/initializers/activerecord_logger.rb
: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 createdconfig/initializers/activerecord_logger.rb
: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.