From 8259a1d8820c5cf685b374a28cf66acecce437d8 Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 12 Sep 2024 22:47:23 +0400 Subject: [PATCH] works but suboptimal due to remanent log_subscribers --- app/controllers/scores_controller.rb | 2 +- config/environments/development.rb | 2 +- config/initializers/hot_changes.rb | 5 ++++- lib/semantic/dev_loader.rb | 24 ++++++++++++++++-------- lib/semantic/subscribers/action_view.rb | 9 +++++---- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 4d8d249..d711872 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -5,7 +5,7 @@ class ScoresController < ApplicationController before_action :set_score, only: %i[show edit update destroy] def index - logger.info('inside controller') + logger.info('inside controller22') @q = Score.all.ransack(q_params) @pagy, @scores = pagy(@q.result) end diff --git a/config/environments/development.rb b/config/environments/development.rb index ad3b2e1..1646fe6 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -89,7 +89,7 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength end elsif Rails.application.server? config.after_initialize do - Semantic::DevLoader.new('toto1') + config.dev_loader = Semantic::DevLoader.new('toto1') end end end diff --git a/config/initializers/hot_changes.rb b/config/initializers/hot_changes.rb index b73bff0..2e997e1 100644 --- a/config/initializers/hot_changes.rb +++ b/config/initializers/hot_changes.rb @@ -2,5 +2,8 @@ return unless Rails.application.server? Rails.application.config.after_initialize do Hot::Constants.on_change(:log_active_record) { |bool| ActiveRecord::Base.logger.level = bool ? :debug : :fatal } - Hot::Constants.on_change(:log_action_view) { |bool| ActionView::Base.logger.level = bool ? :debug : :fatal } + Hot::Constants.on_change(:log_action_view) do |bool| + ActionView::Base.logger.level = bool ? :debug : :fatal + Rails.application.config.dev_loader.launch + end end diff --git a/lib/semantic/dev_loader.rb b/lib/semantic/dev_loader.rb index d49e152..8a38b3b 100644 --- a/lib/semantic/dev_loader.rb +++ b/lib/semantic/dev_loader.rb @@ -3,15 +3,21 @@ module Semantic class DevLoader def initialize(session_key) @session_key = session_key + @subscribers = {} RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal # useful for remanent Rack::Log started + launch + end + + def launch once_and_reload do + append_ansi_formatter + Semantic::NotificationUtil.clear_subscribers(/\.action_controller$/) Semantic::NotificationUtil.clear_subscribers(/\.action_view$/) - - append_ansi_formatter reset_subscribers + register_action_controller register_action_view end @@ -51,17 +57,19 @@ module Semantic end def register_hook(sub_instance, hook, method = hook) - @subscribers << ActiveSupport::Notifications.subscribe("#{hook}.#{sub_instance.event_group}") do |event| + @subscribers[sub_instance.class] ||= [] + @subscribers[sub_instance.class] << ActiveSupport::Notifications.subscribe("#{hook}.#{sub_instance.event_group}") do |event| sub_instance.send(method, event) end end def reset_subscribers - if defined?(@subscribers) - @subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) } - @subscribers.clear - else - @subscribers = [] + return if @subscribers.empty? + + @subscribers.each_pair do |clazz, subs| + # puts "reset #{subs.size} subscribers for class <#{clazz}>" + subs.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) } + subs.clear end end end diff --git a/lib/semantic/subscribers/action_view.rb b/lib/semantic/subscribers/action_view.rb index 36b4e9a..849b959 100644 --- a/lib/semantic/subscribers/action_view.rb +++ b/lib/semantic/subscribers/action_view.rb @@ -7,22 +7,23 @@ module Semantic def initialize super(:action_view) + logger.level = Hot::Constants.log_action_view ? :debug : :fatal end def render_partial(event) - # logger.info('Rendered partial') + # logger.info {'Rendered partial'} end def render_template(event) - logger.debug('Rendered template') + logger.debug { 'Rendered template' } end def render_collection(event) - logger.debug('Rendered collection') + logger.debug { 'Rendered collection' } end def render_layout(event) - logger.debug('Rendered layout') + logger.debug { 'Rendered layout' } end end end