class PaulBunyan::LogSubscriber
Constants
- ACTION_PAYLOAD_KEYS
Handle the
process_action
event in the action_controller namespaceWe're using this to capture the vast majority of our info that goes into the log line
@param event [ActiveSupport::Notifications::Event]
Attributes
action_controller_events[R]
action_view_events[R]
Public Class Methods
event_patterns()
click to toggle source
Build an array of event patterns from the action_controller_events
set for use in finding subscriptions we should add and ones we should remove from the default subscribers
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 56 def self.event_patterns action_controller_events.map{ |event| "#{event}.action_controller" } + action_view_events.map { |event| "#{event}.action_view" } end
subscribe_to_events(notifier = ActiveSupport::Notifications)
click to toggle source
Subscribe to the events we've registered using action_controller_event
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 62 def self.subscribe_to_events(notifier = ActiveSupport::Notifications) subscriber = new notifier = notifier subscribers << subscriber event_patterns.each do |pattern| if Rails::VERSION::MAJOR >= 6 subscriber.patterns[pattern] = notifier.subscribe(pattern, subscriber) else subscriber.patterns << pattern notifier.subscribe(pattern, subscriber) end end end
Public Instance Methods
halted_callback(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 103 def halted_callback(event) aggregator.halting_filter = event.payload[:filter].inspect end
logger()
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 132 def logger PaulBunyan.logger end
process_action(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 93 def process_action(event) payload = event.payload ACTION_PAYLOAD_KEYS.each do |attr| aggregator[attr] = payload[attr] end aggregator.params = payload[:params].except(*INTERNAL_PARAMS) logger.info { aggregator_without_nils } end
redirect_to(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 112 def redirect_to(event) aggregator.redirect_location = event.payload[:location] end
render_partial(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 125 def render_partial(event) aggregator.partials ||= [] aggregator.partials << extract_render_data_from(event) end
Also aliased as: render_collection
render_template(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 121 def render_template(event) aggregator.view = extract_render_data_from(event) end
send_data(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 116 def send_data(event) payload = event.payload aggregator.sent_data = FileTransfer.new(payload[:filename], event.duration) end
send_file(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 107 def send_file(event) payload = event.payload aggregator.sent_file = FileTransfer.new(payload[:path], event.duration) end
start_processing(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 82 def start_processing(event) return end
Private Instance Methods
aggregator()
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 138 def aggregator RequestStore[:logging_request_aggregator] ||= RequestAggregator.new end
aggregator_without_nils()
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 142 def aggregator_without_nils struct_without_nils(aggregator).inject({}) { |data, (key, value)| if Struct === value new_data = {key => struct_without_nils(value)} elsif Array === value new_data = {key => value.map{ |v| struct_without_nils(v) }} else new_data = {key => value} end data.merge(new_data) } end
clean_view_path(path)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 155 def clean_view_path(path) return nil if path.nil? path.sub(rails_root, '').sub(VIEWS_PATTERN, '') end
extract_render_data_from(event)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 160 def extract_render_data_from(event) payload = event.payload RenderedTemplate.new( clean_view_path(payload[:identifier]), event.duration, clean_view_path(payload[:layout]) ) end
rails_root()
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 169 def rails_root @rails_root ||= "#{Rails.root}/" end
struct_without_nils(struct)
click to toggle source
# File lib/paul_bunyan/railtie/log_subscriber.rb, line 173 def struct_without_nils(struct) struct.to_h.reject{ |_, value| value.nil? } end