Module: Logtail::Integrations::Rails::ActiveSupportLogSubscriber

Extended by:
ActiveSupportLogSubscriber
Included in:
ActiveSupportLogSubscriber
Defined in:
lib/logtail-rails/active_support_log_subscriber.rb

Instance Method Summary collapse

Instance Method Details

#find(component, type) ⇒ Object



8
9
10
11
12
# File 'lib/logtail-rails/active_support_log_subscriber.rb', line 8

def find(component, type)
  ::ActiveSupport::LogSubscriber.log_subscribers.find do |subscriber|
    subscriber.class == type
  end
end

#subscribed?(component, type) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/logtail-rails/active_support_log_subscriber.rb', line 14

def subscribed?(component, type)
  !find(component, type).nil?
end

#unsubscribe!(component, type) ⇒ Object

I don’t know why this has to be so complicated, but it is. This code was taken from lograge :/



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/logtail-rails/active_support_log_subscriber.rb', line 20

def unsubscribe!(component, type)
  if defined?(type.detach_from)
    type.detach_from(component)
    return
  end

  subscriber = find(component, type)

  if !subscriber
    raise "We could not find a log subscriber for #{component.inspect} of type #{type.inspect}"
  end

  events = subscriber.public_methods(false).reject { |method| method.to_s == 'call' }
  events.each do |event|
    ::ActiveSupport::Notifications.notifier.listeners_for("#{event}.#{component}").each do |listener|
      if listener.instance_variable_get('@delegate') == subscriber
        ::ActiveSupport::Notifications.unsubscribe listener
      end
    end
  end
end