Browse Source

works but suboptimal due to remanent log_subscribers

main
pvincent 2 months ago
parent
commit
8259a1d882
  1. 2
      app/controllers/scores_controller.rb
  2. 2
      config/environments/development.rb
  3. 5
      config/initializers/hot_changes.rb
  4. 24
      lib/semantic/dev_loader.rb
  5. 9
      lib/semantic/subscribers/action_view.rb

2
app/controllers/scores_controller.rb

@ -5,7 +5,7 @@ class ScoresController < ApplicationController
before_action :set_score, only: %i[show edit update destroy] before_action :set_score, only: %i[show edit update destroy]
def index def index
logger.info('inside controller')
logger.info('inside controller22')
@q = Score.all.ransack(q_params) @q = Score.all.ransack(q_params)
@pagy, @scores = pagy(@q.result) @pagy, @scores = pagy(@q.result)
end end

2
config/environments/development.rb

@ -89,7 +89,7 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength
end end
elsif Rails.application.server? elsif Rails.application.server?
config.after_initialize do config.after_initialize do
Semantic::DevLoader.new('toto1')
config.dev_loader = Semantic::DevLoader.new('toto1')
end end
end end
end end

5
config/initializers/hot_changes.rb

@ -2,5 +2,8 @@ return unless Rails.application.server?
Rails.application.config.after_initialize do 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_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 end

24
lib/semantic/dev_loader.rb

@ -3,15 +3,21 @@ module Semantic
class DevLoader class DevLoader
def initialize(session_key) def initialize(session_key)
@session_key = session_key @session_key = session_key
@subscribers = {}
RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal # useful for remanent Rack::Log started RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal # useful for remanent Rack::Log started
launch
end
def launch
once_and_reload do once_and_reload do
append_ansi_formatter
Semantic::NotificationUtil.clear_subscribers(/\.action_controller$/) Semantic::NotificationUtil.clear_subscribers(/\.action_controller$/)
Semantic::NotificationUtil.clear_subscribers(/\.action_view$/) Semantic::NotificationUtil.clear_subscribers(/\.action_view$/)
append_ansi_formatter
reset_subscribers reset_subscribers
register_action_controller register_action_controller
register_action_view register_action_view
end end
@ -51,17 +57,19 @@ module Semantic
end end
def register_hook(sub_instance, hook, method = hook) 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) sub_instance.send(method, event)
end end
end end
def reset_subscribers 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 end
end end

9
lib/semantic/subscribers/action_view.rb

@ -7,22 +7,23 @@ module Semantic
def initialize def initialize
super(:action_view) super(:action_view)
logger.level = Hot::Constants.log_action_view ? :debug : :fatal
end end
def render_partial(event) def render_partial(event)
# logger.info('Rendered partial')
# logger.info {'Rendered partial'}
end end
def render_template(event) def render_template(event)
logger.debug('Rendered template')
logger.debug { 'Rendered template' }
end end
def render_collection(event) def render_collection(event)
logger.debug('Rendered collection')
logger.debug { 'Rendered collection' }
end end
def render_layout(event) def render_layout(event)
logger.debug('Rendered layout')
logger.debug { 'Rendered layout' }
end end
end end
end end

Loading…
Cancel
Save