Browse Source

Semantic::Subscribers::LogSubscriber

main
pvincent 2 months ago
parent
commit
2abd4d2499
  1. 25
      lib/semantic/dev_loader.rb
  2. 19
      lib/semantic/subscribers/action_controller.rb
  3. 20
      lib/semantic/subscribers/action_view.rb
  4. 18
      lib/semantic/subscribers/log_subscriber.rb

25
lib/semantic/dev_loader.rb

@ -5,6 +5,7 @@ module Semantic
@session_key = session_key @session_key = session_key
RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal # useful for remanent Rack::Log started RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal # useful for remanent Rack::Log started
once_and_reload do once_and_reload do
Semantic::NotificationUtil.clear_subscribers(/\.action_controller$/) Semantic::NotificationUtil.clear_subscribers(/\.action_controller$/)
Semantic::NotificationUtil.clear_subscribers(/\.action_view$/) Semantic::NotificationUtil.clear_subscribers(/\.action_view$/)
@ -12,6 +13,7 @@ module Semantic
append_ansi_formatter append_ansi_formatter
reset_subscribers reset_subscribers
register_action_controller register_action_controller
register_action_view
end end
end end
@ -31,19 +33,26 @@ module Semantic
end end
def register_action_controller def register_action_controller
@log_subscriber = Semantic::Subscribers::ActionController.new(@session_key)
register_action_controller_hook(:start_processing)
register_action_controller_hook(:process_action, :finish_processing)
register_action_controller_hook(:redirect_to)
sub_instance = Semantic::Subscribers::ActionController.new(@session_key)
register_hook(sub_instance, :start_processing)
register_hook(sub_instance, :process_action, :finish_processing)
register_hook(sub_instance, :redirect_to)
%i[send_file send_data halted_callback unpermitted_parameters send_stream write_fragment %i[send_file send_data halted_callback unpermitted_parameters send_stream write_fragment
read_fragment expire_fragment exist_fragment?].each do |hook| read_fragment expire_fragment exist_fragment?].each do |hook|
register_action_controller_hook(hook, :any_hook)
register_hook(sub_instance, hook, :any_hook)
end
end
def register_action_view
sub_instance = Semantic::Subscribers::ActionView.new
%i[render_template render_partial render_collection render_layout].each do |hook|
register_hook(sub_instance, hook)
end end
end end
def register_action_controller_hook(hook, method = hook)
@subscribers << ActiveSupport::Notifications.subscribe("#{hook}.action_controller") do |event|
@log_subscriber.send(method, event)
def register_hook(sub_instance, hook, method = hook)
@subscribers << ActiveSupport::Notifications.subscribe("#{hook}.#{sub_instance.event_group}") do |event|
sub_instance.send(method, event)
end end
end end

19
lib/semantic/subscribers/action_controller.rb

@ -1,22 +1,16 @@
module Semantic module Semantic
module Subscribers module Subscribers
class ActionController < ActiveSupport::LogSubscriber
class ActionController < LogSubscriber
include AnsiColors include AnsiColors
INTERNAL_PARAMS = %i[controller action format _method only_path].freeze INTERNAL_PARAMS = %i[controller action format _method only_path].freeze
DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze
TERMINUS_STRING = '╙─╜'.freeze TERMINUS_STRING = '╙─╜'.freeze
attr_reader :logger
def initialize(session_key) def initialize(session_key)
@session_key = session_key @session_key = session_key
@transactions = {} @transactions = {}
short_name = self.class.to_s.split('::').last
@logger = SemanticLogger[short_name]
super()
super(:action_controller)
end end
def start_processing(event) def start_processing(event)
@ -91,20 +85,11 @@ module Semantic
end end
def redirect_to(event) def redirect_to(event)
SemanticLogger.tagged(@transactions[event.transaction_id]) do
location = capture_path(event.payload[:location]) location = capture_path(event.payload[:location])
logger.debug("Redirected to #{colorize(location, BOLD)}") logger.debug("Redirected to #{colorize(location, BOLD)}")
end
@previously_redirect = true @previously_redirect = true
end end
def any_hook(event)
SemanticLogger.tagged(@transactions[event.transaction_id]) do
logger.warn("action_controller hook=<#{event.name.split('.')[0]}> needs a proper message handling!",
event.payload.keys)
end
end
private private
def redirect_regex def redirect_regex

20
lib/semantic/subscribers/action_view.rb

@ -1,18 +1,28 @@
module Semantic module Semantic
module Subscribers module Subscribers
class ActionView < ActiveSupport::LogSubscriber
class ActionView < LogSubscriber
include AnsiColors include AnsiColors
attr_reader :logger attr_reader :logger
def initialize def initialize
short_name = self.class.to_s.split('::').last
@logger = SemanticLogger[short_name]
super
super(:action_view)
end end
def render_partial(event) def render_partial(event)
logger.info('Rendered partial', event)
# logger.info('Rendered partial')
end
def render_template(event)
logger.debug('Rendered template')
end
def render_collection(event)
logger.debug('Rendered collection')
end
def render_layout(event)
logger.debug('Rendered layout')
end end
end end
end end

18
lib/semantic/subscribers/log_subscriber.rb

@ -0,0 +1,18 @@
module Semantic
module Subscribers
class LogSubscriber
attr_reader :logger, :event_group
def initialize(event_group)
@event_group = event_group
@logger = SemanticLogger[event_group.to_s]
end
def any_hook(event)
logger.warn(
"#{@event_group} hook=<#{event.name.split('.')[0]}> needs a proper message handling!", event.payload.keys
)
end
end
end
end
Loading…
Cancel
Save