Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix log subscriber unsubscriptions #275

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
35 changes: 21 additions & 14 deletions lib/lograge.rb
Expand Up @@ -95,23 +95,30 @@ def ignore?(event)
mattr_accessor :formatter

def remove_existing_log_subscriptions
ActiveSupport::LogSubscriber.log_subscribers.each do |subscriber|
case subscriber
when ActionView::LogSubscriber
unsubscribe(:action_view, subscriber)
when ActionController::LogSubscriber
unsubscribe(:action_controller, subscriber)
end
patterns = ActiveSupport::LogSubscriber.log_subscribers.map(&:patterns).flatten.uniq

patterns.each do |pattern|
subscriber = subscriber_for_pattern(pattern)
next unless subscriber
unsubscribe pattern, subscriber
end
end

def subscriber_for_pattern(pattern)
case pattern.split('.').last
when 'action_controller'
ActionController::LogSubscriber.send(:subscriber)
when 'action_view'
ActionView::LogSubscriber.send(:subscriber)
when 'active_record'
ActiveRecord::LogSubscriber.send(:subscriber)
end
end

def unsubscribe(component, subscriber)
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
def unsubscribe(pattern, subscriber)
ActiveSupport::Notifications.notifier.listeners_for(pattern).each do |listener|
if listener.instance_variable_get('@delegate') == subscriber
ActiveSupport::Notifications.unsubscribe listener
end
end
end
Expand Down