class ActiveSupport::LogSubscriber

request finishes.
that all logs are flushed, and it is called in Rails::Rack::Logger after a
logging. For example, ActiveSupport::LogSubscriber.flush_all! will ensure
ActiveSupport::LogSubscriber also has some helpers to deal with
end
end
end
end
# standard logger code
else
“(#{exception_object.backtrace.first})”
error “[ERROR] #{event.payload}: #{exception.join(‘, ’)} ” <br>
exception_object = event.payload[:exception_object]
if exception
exception = event.payload[:exception]
def sql(event)
class LogSubscriber < ActiveSupport::LogSubscriber
module ActiveRecord
the previous example:
message in case of an error, and this can be achieved by extending
instrumented code raises an exception. It is common to log a different
ActiveSupport::LogSubscriber exposes a simple interface to check if
Being an ActiveSupport::Notifications consumer,
(ActiveSupport::Notifications::Event) to the sql method.
published, it will properly dispatch the event
After configured, whenever a "sql.active_record" notification is
automatically in a Rails environment.
ActiveRecord::LogSubscriber.logger must be set as well, but it is assigned
end
end
end
info “#{event.payload} (#{event.duration}) #{event.payload}”
def sql(event)
attach_to :active_record
class LogSubscriber < ActiveSupport::LogSubscriber
module ActiveRecord
queries:
An example would be Active Record log subscriber responsible for logging
on its given namespace.
The log subscriber dispatches notifications to a registered object based
ActiveSupport::Notifications with the sole purpose of logging them.
ActiveSupport::LogSubscriber is an object set to consume
= Active Support Log Subscriber

def attach_to(...) # :nodoc:

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

def call(event)

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

def color(text, color, mode_options = {}) # :doc:

:doc:
this method will automatically clear formatting at the end of the returned String.
by specifying bold, italic, or underline options. Inspired by Highline,
Set color by using a symbol or one of the defined constants. Set modes
def color(text, color, mode_options = {}) # :doc:
  return text unless colorize_logging
  color = self.class.const_get(color.upcase) if color.is_a?(Symbol)
  mode = mode_from(mode_options)
  clear = "\e[#{MODES[:clear]}m"
  "#{mode}#{color}#{text}#{clear}"
end

def fetch_public_methods(subscriber, inherit_all)

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

def flush_all!

Flush all log_subscribers' logger.
def flush_all!
  logger.flush if logger.respond_to?(:flush)
end

def initialize

def initialize
  super
  @event_levels = {}
end

def log_exception(name, e)

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

def log_subscribers

def log_subscribers
  subscribers
end

def logger

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

def logger

def logger
  LogSubscriber.logger
end

def mode_from(options)

def mode_from(options)
  if options.is_a?(TrueClass) || options.is_a?(FalseClass)
    ActiveSupport.deprecator.warn(<<~MSG.squish)
      Bolding log text with a positional boolean is deprecated and will be removed
      in Rails 7.2. Use an option hash instead (eg. `color("my text", :red, bold: true)`).
    MSG
    options = { bold: options }
  end
  modes = MODES.values_at(*options.compact_blank.keys)
  "\e[#{modes.join(";")}m" if modes.any?
end

def publish_event(event)

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

def set_event_levels

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

def silenced?(event)

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

def subscribe_log_level(method, level)

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