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
# 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

2
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

4
lib/semantic/notification_helper.rb

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