class ActiveSupport::LogSubscriber
(via action_dispatch.callback
notification) in a Rails environment.
flushes all logs when the request finishes
Log subscriber also has some helpers to deal with logging and automatically
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 differentActiveSupport::LogSubscriber
exposes a simple interface to check if
Being an ActiveSupport::Notifications consumer,
(ActiveSupport::Notifications::Event
) to the sql method.
it will properly dispatch the event
After configured, whenever a "sql.active_record"
notification is published,
This is assigned automatically in a Rails environment.
A logger also needs to be set with ActiveRecord::LogSubscriber.logger=
.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
info “#{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) # :doc:
end of the returned String.
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) # :doc: 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 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 finish(name, id, payload)
def finish(name, id, payload) super if logger rescue => e log_exception(name, e) end
def flush_all!
def flush_all! logger.flush if logger.respond_to?(:flush) 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 publish_event(event)
def publish_event(event) super if logger rescue => e log_exception(event.name, e) end
def start(name, id, payload)
def start(name, id, payload) super if logger end