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]
|
|
|
|
message << " (#{event.duration.round(1)}ms)"
|
|
|
|
end
|
2010-06-24 07:23:43 -04:00
|
|
|
end
|
2016-07-14 06:38:16 -04:00
|
|
|
|
|
|
|
def render_partial(event)
|
|
|
|
info 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]
|
|
|
|
message << " (#{event.duration.round(1)}ms)"
|
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
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
info do
|
|
|
|
" Rendered collection of #{from_rails_root(identifier)}" \
|
|
|
|
" #{render_count(event.payload)} (#{event.duration.round(1)}ms)"
|
|
|
|
end
|
|
|
|
end
|
2010-06-24 07:23:43 -04:00
|
|
|
|
2016-02-26 13:23:59 -05:00
|
|
|
def start(name, id, payload)
|
|
|
|
if name == "render_template.action_view"
|
|
|
|
log_rendering_start(payload)
|
2016-02-16 12:56:33 -05:00
|
|
|
end
|
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
|
2010-06-24 07:23:43 -04:00
|
|
|
|
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
|
|
|
|
|
2016-02-26 13:23:59 -05:00
|
|
|
def log_rendering_start(payload)
|
|
|
|
info do
|
2018-05-17 04:32:27 -04:00
|
|
|
message = +" Rendering #{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
|