From 452af8c31f8e39f8a51f8412fd538bbd41f4dccc Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 11 Sep 2024 22:14:37 +0400 Subject: [PATCH] clear_subscribers --- lib/semantic/dev_loader.rb | 28 +++++++++++++++++++++++++++- lib/semantic/log_subscriber.rb | 2 +- lib/semantic/notification_helper.rb | 4 ++++ 3 files changed, 32 insertions(+), 2 deletions(-) create mode 100644 lib/semantic/notification_helper.rb diff --git a/lib/semantic/dev_loader.rb b/lib/semantic/dev_loader.rb index 1d44ef3..cb530ca 100644 --- a/lib/semantic/dev_loader.rb +++ b/lib/semantic/dev_loader.rb @@ -11,7 +11,8 @@ module Semantic end # FIXME: proper unsubscribe!!! - RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal + # RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal + # # RailsSemanticLogger.swap_subscriber( # RailsSemanticLogger::ActionController::LogSubscriber, @@ -62,7 +63,32 @@ 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 'remove notification then log_subscriber' + + ActiveSupport::Notifications.unsubscribe(sub) + ActiveSupport::LogSubscriber.subscribers.delete(subscriber) + end + end + 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 + clear_subscribers(/\.action_controller$/) if defined?(@subscribers) @subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) } @subscribers.clear diff --git a/lib/semantic/log_subscriber.rb b/lib/semantic/log_subscriber.rb index 3407393..e2df119 100644 --- a/lib/semantic/log_subscriber.rb +++ b/lib/semantic/log_subscriber.rb @@ -2,7 +2,7 @@ module Semantic class LogSubscriber < ActiveSupport::LogSubscriber include SemanticLogger::Loggable include AnsiColors - def logger = SemanticLogger['Rails'] + def logger = SemanticLogger['IRails'] INTERNAL_PARAMS = %i[controller action format _method only_path].freeze DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze diff --git a/lib/semantic/notification_helper.rb b/lib/semantic/notification_helper.rb new file mode 100644 index 0000000..3353e25 --- /dev/null +++ b/lib/semantic/notification_helper.rb @@ -0,0 +1,4 @@ +module Semantic + module NotificationHelper + end +end