From da6d4c1376fe01ac0de513edbdf7eb9a9786ee85 Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 12 Sep 2024 12:43:37 +0400 Subject: [PATCH] notification_util --- config/environments/development.rb | 1 + lib/semantic/dev_loader.rb | 32 +++-------------------- lib/semantic/notification_helper.rb | 4 --- lib/semantic/notification_util.rb | 39 +++++++++++++++++++++++++++++ 4 files changed, 43 insertions(+), 33 deletions(-) delete mode 100644 lib/semantic/notification_helper.rb create mode 100644 lib/semantic/notification_util.rb diff --git a/config/environments/development.rb b/config/environments/development.rb index f89e870..ad3b2e1 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -82,6 +82,7 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength formatter = Semantic::BasicFormatter.new SemanticLogger.add_appender(io: $stdout, formatter:) + # FIXME: filter is useful! # SemanticLogger.add_appender(io: $stdout, # formatter:, # filter: ->(log) { !formatter.reject(log) }) diff --git a/lib/semantic/dev_loader.rb b/lib/semantic/dev_loader.rb index 7dfe5c7..f9ed82b 100644 --- a/lib/semantic/dev_loader.rb +++ b/lib/semantic/dev_loader.rb @@ -8,8 +8,8 @@ module Semantic RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal # useful for remanent Rack::Log started once_and_reload do - clear_subscribers(/\.action_controller$/) - clear_subscribers(/\.action_view$/) + NotificationUtil.clear_subscribers(/\.action_controller$/) + NotificationUtil.clear_subscribers(/\.action_view$/) append_ansi_formatter reset_subscribers @@ -52,35 +52,9 @@ module Semantic end end - def subscriber_patterns(subscriber) - subscriber.patterns.respond_to?(:keys) ? subscriber.patterns.keys : subscriber.patterns - end - - def unattach(subscriber, pattern) - subscriber_patterns(subscriber).each do |sub_pattern| - ActiveSupport::Notifications.notifier.listeners_for(sub_pattern).each do |sub| - next unless sub.instance_variable_get(:@delegate) == subscriber - next unless pattern.match(sub_pattern) - - puts "FOUND subscriber=#{subscriber} for sub_pattern=#{sub_pattern} with logger #{subscriber.logger.name}" - puts subscriber.class.module_parent.const_source_location(subscriber.class.to_s)&.first - - ActiveSupport::Notifications.unsubscribe(sub) - end - end - ActiveSupport::LogSubscriber.subscribers.delete(subscriber) - end - - # pattern could be either a string 'start_processing.action_controller' or a regex /\.action_controller$/ - def clear_subscribers(pattern) - ActiveSupport::LogSubscriber.subscribers.each { |sub| unattach(sub, pattern) } - end - def reset_subscribers if defined?(@subscribers) - @subscribers.each do |sub| - ActiveSupport::Notifications.unsubscribe(sub) - end + @subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) } @subscribers.clear else @subscribers = [] diff --git a/lib/semantic/notification_helper.rb b/lib/semantic/notification_helper.rb deleted file mode 100644 index 3353e25..0000000 --- a/lib/semantic/notification_helper.rb +++ /dev/null @@ -1,4 +0,0 @@ -module Semantic - module NotificationHelper - end -end diff --git a/lib/semantic/notification_util.rb b/lib/semantic/notification_util.rb new file mode 100644 index 0000000..2c7c4b5 --- /dev/null +++ b/lib/semantic/notification_util.rb @@ -0,0 +1,39 @@ +module Semantic + module NotificationUtil + class << self + # pattern could be either a string 'start_processing.action_controller' or a regex /\.action_controller$/ + # FIXME: weird behaviour, order impact!!!! + # For instance: + # OK + # NotificationUtil.clear_subscribers(/\.action_controller$/) + # NotificationUtil.clear_subscribers(/\.action_view$/) + # NOPE + # NotificationUtil.clear_subscribers(/\.action_view$/) + # NotificationUtil.clear_subscribers(/\.action_controller$/) + def clear_subscribers(pattern) + ActiveSupport::LogSubscriber.subscribers.each { |sub| unattach(sub, pattern) } + end + + private + + def subscriber_patterns(subscriber) + subscriber.patterns.respond_to?(:keys) ? subscriber.patterns.keys : subscriber.patterns + end + + def unattach(subscriber, pattern) + subscriber_patterns(subscriber).each do |sub_pattern| + ActiveSupport::Notifications.notifier.listeners_for(sub_pattern).each do |sub| + next unless sub.instance_variable_get(:@delegate) == subscriber + next unless pattern.match(sub_pattern) + + puts "FOUND subscriber=#{subscriber} for sub_pattern=#{sub_pattern} with logger #{subscriber.logger.name}" + puts subscriber.class.module_parent.const_source_location(subscriber.class.to_s)&.first + + ActiveSupport::Notifications.unsubscribe(sub) + end + end + # ActiveSupport::LogSubscriber.subscribers.delete(subscriber) + end + end + end +end