Browse Source

notification_util

main
pvincent 3 months ago
parent
commit
da6d4c1376
  1. 1
      config/environments/development.rb
  2. 32
      lib/semantic/dev_loader.rb
  3. 4
      lib/semantic/notification_helper.rb
  4. 39
      lib/semantic/notification_util.rb

1
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) })

32
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 = []

4
lib/semantic/notification_helper.rb

@ -1,4 +0,0 @@
module Semantic
module NotificationHelper
end
end

39
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
Loading…
Cancel
Save