【问题标题】:How to show Rails ActiveRecord query time in Heroku pry rails console?如何在 Heroku pry rails 控制台中显示 Rails ActiveRecord 查询时间?
【发布时间】:2021-03-11 18:17:22
【问题描述】:

当我在本地控制台中运行 ActiveRecord 查询时,我会看到查询运行所需的时间(以毫秒为单位)以及从我的 ActiveRecord 代码生成的原始 SQL。例如:

$ rails c
Running via Spring preloader in process 92420
Loading development environment (Rails 6.1.3)
[1] pry(main)> TableName.count
   (26336.8ms)  SELECT COUNT(*) FROM "table_names"
=> 10005457

但是当我在 Heroku rails 控制台中运行相同的查询时,我看不到查询时间或 SQL,只看到结果。示例:

$ heroku run rails c
Running rails c on ⬢ app-name... up, run.9115 (Free)
Loading production environment (Rails 6.1.3)
[1] pry(main)> TableName.count
=> 10005457

如果相关,我通过我的 Gemfile 安装了 gem 'pry-byebug'gem 'pry-rails'不是development 组中)。我也使用 PostgreSQL 作为我的数据库。

【问题讨论】:

    标签: ruby-on-rails heroku activerecord pry pry-rails


    【解决方案1】:

    是否显示查询时间和SQL取决于Log Levels

    当日志级别为:debug或对应数字0时显示。

    您可以在config/environments/production.rb 中查看config.log_level

    如果是:debug,那么可能是 Heroku 以某种方式更改了日志级别。

    您可以通过Rails.logger.level在Hereku控制台查看日志级别。

    并通过Rails.logger.level = :debug将日志级别设置为:debug

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-20
      • 2011-02-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-21
      相关资源
      最近更新 更多