ruby-on-rails 如何在Rails日志中显示视图文件的完整路径?

ou6hu8tu  于 2023-04-08  发布在  Ruby
关注(0)|答案(1)|浏览(89)

在Rails日志中,我们可以看到这些数据。

I, [2023-04-03T18:04:57.968359 #77096]  INFO -- : Started GET "/" for ::1 at 2023-04-03 18:04:57 +0100
D, [2023-04-03T18:04:57.991136 #77096] DEBUG -- :   ActiveRecord::SchemaMigration Pluck (0.2ms)  SELECT `schema_migrations`.`version` FROM `schema_migrations` ORDER BY `schema_migrations`.`version` ASC
I, [2023-04-03T18:04:58.011691 #77096]  INFO -- : Processing by HomeController#index as HTML
D, [2023-04-03T18:04:58.017021 #77096] DEBUG -- :   Rendering layout layouts/application.html.erb
D, [2023-04-03T18:04:58.017077 #77096] DEBUG -- :   Rendering home/index.html.erb within layouts/application
I, [2023-04-03T18:04:58.018070 #77096]  INFO -- :   Rendered home/index.html.erb within layouts/application (Duration: 1.0ms | Allocations: 922)
I, [2023-04-03T18:04:58.054433 #77096]  INFO -- :   Rendered layout layouts/application.html.erb (Duration: 37.4ms | Allocations: 60188)
I, [2023-04-03T18:04:58.054628 #77096]  INFO -- : Completed 200 OK in 43ms (Views: 38.8ms | ActiveRecord: 0.0ms | Allocations: 65440)

我想看到视图文件的完整路径,当它被渲染时。
因此,我希望它不是Rendered home/index.html.erb,而是Rendered app/views/home/index.html.erb
我如何才能做到这一点?
目前我在config/environments/development.rb中对logger的设置是这样的。

config.log_level = :debug
  config.log_formatter = Logger::Formatter.new
0yycz8jy

0yycz8jy1#

您可以对ActionView::LogsSubscriber类中定义的名为from_rails_root的私有方法进行Monkey修补,该方法生成视图文件路径:

require 'action_view'

module ActionView::LogsSubscriber
  private

    def from_rails_root(string) # :doc:
      string = string.sub(rails_root, EMPTY)
      # This line has to be removed in order to keep the 'app/views' string in the path.
      # string.sub!(VIEWS_PATTERN, EMPTY)
      string
    end
end

或者,您可以通过创建自定义日志记录器类来覆盖操作视图的日志记录器配置:

# config/environments/development.rb

config.action_view.logger = MyCustomViewLogger

相关问题