2017-07-01 16:09:13 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:48:09 -04:00
|
|
|
|
2016-08-06 12:48:35 -04:00
|
|
|
require "active_support/log_subscriber"
|
2013-07-25 10:05:22 -04:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
module ActionView
|
|
|
|
# = Action View Log Subscriber
|
|
|
|
#
|
|
|
|
# Provides functionality so that Rails can output logs from Action View.
|
|
|
|
class LogSubscriber < ActiveSupport::LogSubscriber
|
2013-11-06 17:37:30 -05:00
|
|
|
VIEWS_PATTERN = /^app\/views\//
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@root = nil
|
|
|
|
super
|
|
|
|
end
|
2012-09-19 19:00:28 -04:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
def render_template(event)
|
2014-07-18 02:27:08 -04:00
|
|
|
info do
|
2018-05-17 04:32:27 -04:00
|
|
|
message = +" Rendered #{from_rails_root(event.payload[:identifier])}"
|
2014-07-18 02:27:08 -04:00
|
|
|
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
|
2018-10-09 14:08:25 -04:00
|
|
|
message << " (Duration: #{event.duration.round(1)}ms | Allocations: #{event.allocations})"
|
2014-07-18 02:27:08 -04:00
|
|
|
end
|
2010-06-24 07:23:43 -04:00
|
|
|
end
|
2016-07-14 06:38:16 -04:00
|
|
|
|
|
|
|
def render_partial(event)
|
2019-08-25 14:39:58 -04:00
|
|
|
debug do
|
2018-05-17 04:32:27 -04:00
|
|
|
message = +" Rendered #{from_rails_root(event.payload[:identifier])}"
|
2016-07-14 06:38:16 -04:00
|
|
|
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
|
2018-10-09 14:08:25 -04:00
|
|
|
message << " (Duration: #{event.duration.round(1)}ms | Allocations: #{event.allocations})"
|
2017-06-07 16:17:34 -04:00
|
|
|
message << " #{cache_message(event.payload)}" unless event.payload[:cache_hit].nil?
|
2016-07-14 06:38:16 -04:00
|
|
|
message
|
|
|
|
end
|
|
|
|
end
|
2016-02-18 15:55:42 -05:00
|
|
|
|
2020-04-20 16:28:35 -04:00
|
|
|
def render_layout(event)
|
|
|
|
info do
|
|
|
|
message = +" Rendered layout #{from_rails_root(event.payload[:identifier])}"
|
|
|
|
message << " (Duration: #{event.duration.round(1)}ms | Allocations: #{event.allocations})"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-18 15:55:42 -05:00
|
|
|
def render_collection(event)
|
2016-08-06 12:48:35 -04:00
|
|
|
identifier = event.payload[:identifier] || "templates"
|
2016-02-18 15:55:42 -05:00
|
|
|
|
2019-08-25 14:39:58 -04:00
|
|
|
debug do
|
2020-04-20 16:28:35 -04:00
|
|
|
message = +" Rendered collection of #{from_rails_root(identifier)}"
|
|
|
|
message << " within #{from_rails_root(event.payload[:layout])}" if event.payload[:layout]
|
|
|
|
message << " #{render_count(event.payload)} (Duration: #{event.duration.round(1)}ms | Allocations: #{event.allocations})"
|
|
|
|
message
|
2016-02-18 15:55:42 -05:00
|
|
|
end
|
|
|
|
end
|
2010-06-24 07:23:43 -04:00
|
|
|
|
2016-02-26 13:23:59 -05:00
|
|
|
def start(name, id, payload)
|
2020-04-20 16:28:35 -04:00
|
|
|
log_rendering_start(payload, name)
|
2016-02-26 13:23:59 -05:00
|
|
|
|
|
|
|
super
|
2016-02-16 12:56:33 -05:00
|
|
|
end
|
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
def logger
|
2012-01-18 12:05:36 -05:00
|
|
|
ActionView::Base.logger
|
2010-06-24 07:23:43 -04:00
|
|
|
end
|
|
|
|
|
2016-12-19 05:30:36 -05:00
|
|
|
private
|
2016-08-06 12:48:35 -04:00
|
|
|
EMPTY = ""
|
2016-12-19 05:30:36 -05:00
|
|
|
def from_rails_root(string) # :doc:
|
2013-11-09 15:24:49 -05:00
|
|
|
string = string.sub(rails_root, EMPTY)
|
|
|
|
string.sub!(VIEWS_PATTERN, EMPTY)
|
|
|
|
string
|
2013-11-06 17:37:30 -05:00
|
|
|
end
|
|
|
|
|
2016-12-19 05:30:36 -05:00
|
|
|
def rails_root # :doc:
|
2013-11-06 17:37:30 -05:00
|
|
|
@root ||= "#{Rails.root}/"
|
2010-06-24 07:23:43 -04:00
|
|
|
end
|
2016-02-18 15:55:42 -05:00
|
|
|
|
2016-12-19 05:30:36 -05:00
|
|
|
def render_count(payload) # :doc:
|
2016-02-18 15:55:42 -05:00
|
|
|
if payload[:cache_hits]
|
|
|
|
"[#{payload[:cache_hits]} / #{payload[:count]} cache hits]"
|
|
|
|
else
|
|
|
|
"[#{payload[:count]} times]"
|
|
|
|
end
|
|
|
|
end
|
2016-02-26 13:23:59 -05:00
|
|
|
|
2016-12-19 05:30:36 -05:00
|
|
|
def cache_message(payload) # :doc:
|
2017-06-07 16:17:34 -04:00
|
|
|
case payload[:cache_hit]
|
|
|
|
when :hit
|
2016-07-14 06:38:16 -04:00
|
|
|
"[cache hit]"
|
2017-06-07 16:17:34 -04:00
|
|
|
when :miss
|
2016-07-14 06:38:16 -04:00
|
|
|
"[cache miss]"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-20 16:28:35 -04:00
|
|
|
def log_rendering_start(payload, name)
|
2019-08-25 14:39:58 -04:00
|
|
|
debug do
|
2020-04-20 16:28:35 -04:00
|
|
|
qualifier =
|
|
|
|
if name == "render_template.action_view"
|
|
|
|
""
|
|
|
|
elsif name == "render_layout.action_view"
|
|
|
|
"layout "
|
|
|
|
end
|
|
|
|
|
|
|
|
return unless qualifier
|
|
|
|
|
|
|
|
message = +" Rendering #{qualifier}#{from_rails_root(payload[:identifier])}"
|
2016-02-26 13:23:59 -05:00
|
|
|
message << " within #{from_rails_root(payload[:layout])}" if payload[:layout]
|
|
|
|
message
|
|
|
|
end
|
|
|
|
end
|
2010-06-24 07:23:43 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-06-27 18:46:57 -04:00
|
|
|
ActionView::LogSubscriber.attach_to :action_view
|