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



在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

我怎样才能做到这一点?

当前我在配置/环境/开发中设置的记录器。Rb是这样的

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

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

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

最新更新