class ActiveSupport::LogSubscriber
notification) in a Rails environment.
flushes all logs when the request finishes (via action_dispatch.callback
Log subscriber also has some helpers to deal with logging and automatically
the sql method.
it will properly dispatch the event (ActiveSupport::Notifications::Event) to
After configured, whenever a “sql.active_record” notification is published,ActiveRecord::LogSubscriber
definition.
subscriber, the line above should be called after your
Since we need to know all instance methods before attaching the log
ActiveRecord::LogSubscriber.attach_to :active_record
And it’s finally registered as:
end
end
end
“#{event.payload} (#{event.duration}) #{event.payload}”
def sql(event)
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
def color(text, color, bold=false)
on the Highline implementation and will automatically append CLEAR to the
option is set to +true+, it also adds bold to the string. This is based
Set color by using a symbol or one of the defined constants. If a third
def color(text, color, bold=false) return text unless colorize_logging color = self.class.const_get(color.upcase) if color.is_a?(Symbol) bold = bold ? BOLD : "" "#{bold}#{color}#{text}#{CLEAR}" end
def finish(name, id, payload)
def finish(name, id, payload) super if logger rescue Exception => e logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}" end
def flush_all!
def flush_all! logger.flush if logger.respond_to?(:flush) 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 start(name, id, payload)
def start(name, id, payload) super if logger end