123456789_123456789_123456789_123456789_123456789_

Class: ActiveSupport::LogSubscriber

Do not use. This class is for internal use only.
Relationships & Source Files
Namespace Children
Modules:
Extension / Inclusion / Inheritance Descendants
Subclasses:
Super Chains via Extension / Inclusion / Inheritance
Class Chain:
self, Subscriber
Instance Chain:
Inherits: ActiveSupport::Subscriber
Defined in: activesupport/lib/active_support/log_subscriber.rb,
activesupport/lib/active_support/log_subscriber/test_helper.rb

Overview

LogSubscriber is an object set to consume Notifications with the sole purpose of logging them. The log subscriber dispatches notifications to a registered object based on its given namespace.

An example would be Active Record log subscriber responsible for logging queries:

module ActiveRecord
  class LogSubscriber < ActiveSupport::LogSubscriber
    attach_to :active_record

    def sql(event)
      info "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}"
    end
  end
end

ActiveRecord::LogSubscriber.logger must be set as well, but it is assigned automatically in a Rails environment.

After configured, whenever a "sql.active_record" notification is published, it will properly dispatch the event (ActiveSupport::Notifications::Event) to the sql method.

Being an Notifications consumer, LogSubscriber exposes a simple interface to check if instrumented code raises an exception. It is common to log a different message in case of an error, and this can be achieved by extending the previous example:

module ActiveRecord
  class LogSubscriber < ActiveSupport::LogSubscriber
    def sql(event)
      exception = event.payload[:exception]

      if exception
        exception_object = event.payload[:exception_object]

        error "[ERROR] #{event.payload[:name]}: #{exception.join(', ')} " \
              "(#{exception_object.backtrace.first})"
      else
        # standard logger code
      end
    end
  end
end

LogSubscriber also has some helpers to deal with logging. For example, .flush_all! will ensure that all logs are flushed, and it is called in ::Rails::Rack::Logger after a request finishes.

Constant Summary

ColorizeLogging - Included

BLACK, BLUE, CYAN, GREEN, MAGENTA, MODES, RED, WHITE, YELLOW

Class Attribute Summary

Class Method Summary

Subscriber - Inherited

.attach_to

Attach the subscriber to a namespace.

.detach_from

Detach the subscriber from a namespace.

.method_added

Adds event subscribers for all new methods added to the class.

.new, .subscribers, .add_event_subscriber, .fetch_public_methods, .find_attached_subscriber, .invalid_event?, .pattern_subscribed?, .prepare_pattern, .remove_event_subscriber

Instance Attribute Summary

Instance Method Summary

Constructor Details

.newLogSubscriber

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 120

def initialize
  super
  @event_levels = {}
end

Class Attribute Details

.log_levels (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 67

class_attribute :log_levels, instance_accessor: false, default: {} # :nodoc:

.log_levels?Boolean (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 67

class_attribute :log_levels, instance_accessor: false, default: {} # :nodoc:

.logger (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 76

def logger
  @logger ||= if defined?(Rails) && Rails.respond_to?(:logger)
    Rails.logger
  end
end

.logger=(value) (rw)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 82

attr_writer :logger

Class Method Details

.attach_to

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 84

def attach_to(...) # :nodoc:
  result = super
  set_event_levels
  result
end

.fetch_public_methods(subscriber, inherit_all) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 100

def fetch_public_methods(subscriber, inherit_all)
  subscriber.public_methods(inherit_all) - LogSubscriber.public_instance_methods(true)
end

.flush_all!

Flush all log_subscribers’ logger.

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 95

def flush_all!
  logger.flush if logger.respond_to?(:flush)
end

.log_subscribers

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 90

def log_subscribers
  subscribers
end

.set_event_levels (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 104

def set_event_levels
  if subscriber
    subscriber.event_levels = log_levels.transform_keys { |k| "#{k}.#{namespace}" }
  end
end

.subscribe_log_level(method, level) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 110

def subscribe_log_level(method, level)
  self.log_levels = log_levels.merge(method => LEVEL_CHECKS.fetch(level))
  set_event_levels
end

Instance Attribute Details

#event_levels=(value) (writeonly)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 135

attr_writer :event_levels # :nodoc:

Instance Method Details

#call(event)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 129

def call(event)
  super if logger
rescue => e
  log_exception(event.name, e)
end

#log_exception(name, e) (private)

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 138

def log_exception(name, e)
  ActiveSupport.error_reporter.report(e, source: name)

  if logger
    logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}"
  end
end

#logger

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 116

def logger
  LogSubscriber.logger
end

#silenced?(event) ⇒ Boolean

[ GitHub ]

  
# File 'activesupport/lib/active_support/log_subscriber.rb', line 125

def silenced?(event)
  logger.nil? || @event_levels[event]&.call(logger)
end