Browse Source

clear_subscribers

main
pvincent 2 months ago
parent
commit
452af8c31f
  1. 28
      lib/semantic/dev_loader.rb
  2. 2
      lib/semantic/log_subscriber.rb
  3. 4
      lib/semantic/notification_helper.rb

28
lib/semantic/dev_loader.rb

@ -11,7 +11,8 @@ module Semantic
end end
# FIXME: proper unsubscribe!!! # FIXME: proper unsubscribe!!!
RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal
# RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal
# #
# RailsSemanticLogger.swap_subscriber( # RailsSemanticLogger.swap_subscriber(
# RailsSemanticLogger::ActionController::LogSubscriber, # RailsSemanticLogger::ActionController::LogSubscriber,
@ -62,7 +63,32 @@ module Semantic
end end
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 def reset_subscribers
clear_subscribers(/\.action_controller$/)
if defined?(@subscribers) if defined?(@subscribers)
@subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) } @subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
@subscribers.clear @subscribers.clear

2
lib/semantic/log_subscriber.rb

@ -2,7 +2,7 @@ module Semantic
class LogSubscriber < ActiveSupport::LogSubscriber class LogSubscriber < ActiveSupport::LogSubscriber
include SemanticLogger::Loggable include SemanticLogger::Loggable
include AnsiColors include AnsiColors
def logger = SemanticLogger['Rails']
def logger = SemanticLogger['IRails']
INTERNAL_PARAMS = %i[controller action format _method only_path].freeze INTERNAL_PARAMS = %i[controller action format _method only_path].freeze
DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze DEFAULT_DEV_HOSTS = ['127.0.0.1', 'localhost'].freeze

4
lib/semantic/notification_helper.rb

@ -0,0 +1,4 @@
module Semantic
module NotificationHelper
end
end
Loading…
Cancel
Save