2013-07-25 10:05:22 -04:00
|
|
|
require 'active_support/log_subscriber'
|
|
|
|
|
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
|
|
|
|
message = " Rendered #{from_rails_root(event.payload[:identifier])}"
|
|
|
|
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
|
|
|
|
alias :render_partial :render_template
|
2016-02-18 15:55:42 -05:00
|
|
|
|
|
|
|
def render_collection(event)
|
|
|
|
identifier = event.payload[:identifier] || 'templates'
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2013-11-06 17:37:30 -05:00
|
|
|
EMPTY = ''
|
2010-06-24 07:23:43 -04:00
|
|
|
def from_rails_root(string)
|
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
|
|
|
|
|
|
|
|
def rails_root
|
|
|
|
@root ||= "#{Rails.root}/"
|
2010-06-24 07:23:43 -04:00
|
|
|
end
|
2016-02-18 15:55:42 -05:00
|
|
|
|
|
|
|
def render_count(payload)
|
|
|
|
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
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def log_rendering_start(payload)
|
|
|
|
info do
|
|
|
|
message = " Rendering #{from_rails_root(payload[:identifier])}"
|
|
|
|
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
|