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]
def index
logger.info('inside controller')
logger.info('inside controller22')
@q = Score.all.ransack(q_params)
@pagy, @scores = pagy(@q.result)
end

2
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

5
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

24
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

9
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

Loading…
Cancel
Save