2010-06-24 07:23:43 -04:00
|
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
|
|
|
require 'active_support/core_ext/class/attribute'
|
2010-01-12 06:52:57 -05:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
module ActiveSupport
|
|
|
|
# ActiveSupport::LogSubscriber is an object set to consume ActiveSupport::Notifications
|
2011-06-22 09:27:22 -04:00
|
|
|
# with the sole purpose of logging them. The log subscriber dispatches notifications to
|
|
|
|
# a registered object based on its given namespace.
|
2010-01-12 06:52:57 -05:00
|
|
|
#
|
2010-06-14 17:21:53 -04:00
|
|
|
# An example would be Active Record log subscriber responsible for logging queries:
|
2010-01-12 06:52:57 -05:00
|
|
|
#
|
|
|
|
# module ActiveRecord
|
2010-06-24 07:23:43 -04:00
|
|
|
# class LogSubscriber < ActiveSupport::LogSubscriber
|
|
|
|
# def sql(event)
|
|
|
|
# "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}"
|
2010-01-12 06:52:57 -05:00
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
#
|
2010-08-14 11:52:52 -04:00
|
|
|
# And it's finally registered as:
|
2010-01-12 06:52:57 -05:00
|
|
|
#
|
2010-06-24 07:23:43 -04:00
|
|
|
# ActiveRecord::LogSubscriber.attach_to :active_record
|
2010-01-12 06:52:57 -05:00
|
|
|
#
|
2010-06-24 07:23:43 -04:00
|
|
|
# Since we need to know all instance methods before attaching the log subscriber,
|
2011-03-06 04:44:52 -05:00
|
|
|
# the line above should be called after your <tt>ActiveRecord::LogSubscriber</tt> definition.
|
2010-06-24 07:23:43 -04:00
|
|
|
#
|
|
|
|
# After configured, whenever a "sql.active_record" notification is published,
|
2010-01-12 06:52:57 -05:00
|
|
|
# it will properly dispatch the event (ActiveSupport::Notifications::Event) to
|
|
|
|
# the sql method.
|
|
|
|
#
|
2010-02-15 09:44:30 -05:00
|
|
|
# Log subscriber also has some helpers to deal with logging and automatically flushes
|
2010-06-24 07:23:43 -04:00
|
|
|
# all logs when the request finishes (via action_dispatch.callback notification) in
|
|
|
|
# a Rails environment.
|
2010-02-15 09:44:30 -05:00
|
|
|
class LogSubscriber
|
2010-06-24 07:23:43 -04:00
|
|
|
# Embed in a String to clear all previous ANSI sequences.
|
|
|
|
CLEAR = "\e[0m"
|
|
|
|
BOLD = "\e[1m"
|
2010-08-14 01:13:00 -04:00
|
|
|
|
2010-01-12 06:52:57 -05:00
|
|
|
# Colors
|
2010-06-24 07:23:43 -04:00
|
|
|
BLACK = "\e[30m"
|
|
|
|
RED = "\e[31m"
|
|
|
|
GREEN = "\e[32m"
|
|
|
|
YELLOW = "\e[33m"
|
|
|
|
BLUE = "\e[34m"
|
|
|
|
MAGENTA = "\e[35m"
|
|
|
|
CYAN = "\e[36m"
|
|
|
|
WHITE = "\e[37m"
|
2010-01-12 06:52:57 -05:00
|
|
|
|
2010-08-15 07:41:01 -04:00
|
|
|
mattr_accessor :colorize_logging
|
|
|
|
self.colorize_logging = true
|
2010-01-12 06:52:57 -05:00
|
|
|
|
2010-08-15 07:41:01 -04:00
|
|
|
class_attribute :logger
|
2010-07-18 20:37:39 -04:00
|
|
|
|
2010-08-15 07:41:01 -04:00
|
|
|
class << self
|
|
|
|
remove_method :logger
|
|
|
|
def logger
|
|
|
|
@logger ||= Rails.logger if defined?(Rails)
|
2010-01-12 06:52:57 -05:00
|
|
|
end
|
2010-01-14 16:03:47 -05:00
|
|
|
|
2010-08-15 07:41:01 -04:00
|
|
|
def attach_to(namespace, log_subscriber=new, notifier=ActiveSupport::Notifications)
|
|
|
|
log_subscribers << log_subscriber
|
|
|
|
@@flushable_loggers = nil
|
|
|
|
|
|
|
|
log_subscriber.public_methods(false).each do |event|
|
|
|
|
next if 'call' == event.to_s
|
2010-03-02 16:05:25 -05:00
|
|
|
|
2010-08-15 07:41:01 -04:00
|
|
|
notifier.subscribe("#{event}.#{namespace}", log_subscriber)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_subscribers
|
|
|
|
@@log_subscribers ||= []
|
2010-06-04 12:48:29 -04:00
|
|
|
end
|
|
|
|
|
2010-08-15 07:41:01 -04:00
|
|
|
def flushable_loggers
|
|
|
|
@@flushable_loggers ||= begin
|
|
|
|
loggers = log_subscribers.map(&:logger)
|
|
|
|
loggers.uniq!
|
|
|
|
loggers.select { |l| l.respond_to?(:flush) }
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Flush all log_subscribers' logger.
|
|
|
|
def flush_all!
|
2011-04-26 13:41:59 -04:00
|
|
|
flushable_loggers.each { |log| log.flush }
|
2010-08-15 07:41:01 -04:00
|
|
|
end
|
2010-01-12 06:52:57 -05:00
|
|
|
end
|
|
|
|
|
2010-07-18 20:37:39 -04:00
|
|
|
def call(message, *args)
|
|
|
|
return unless logger
|
|
|
|
|
|
|
|
method = message.split('.').first
|
|
|
|
begin
|
|
|
|
send(method, ActiveSupport::Notifications::Event.new(message, *args))
|
|
|
|
rescue Exception => e
|
|
|
|
logger.error "Could not log #{message.inspect} event. #{e.class}: #{e.message}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-12 06:52:57 -05:00
|
|
|
protected
|
|
|
|
|
|
|
|
%w(info debug warn error fatal unknown).each do |level|
|
|
|
|
class_eval <<-METHOD, __FILE__, __LINE__ + 1
|
|
|
|
def #{level}(*args, &block)
|
2010-03-03 19:22:30 -05:00
|
|
|
return unless logger
|
2010-01-12 06:52:57 -05:00
|
|
|
logger.#{level}(*args, &block)
|
|
|
|
end
|
|
|
|
METHOD
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set color by using a string or one of the defined constants. If a third
|
|
|
|
# option is set to true, it also adds bold to the string. This is based
|
2011-06-22 09:27:22 -04:00
|
|
|
# on the Highline implementation and will automatically append CLEAR to the
|
|
|
|
# end of the returned String.
|
2010-01-12 06:52:57 -05:00
|
|
|
#
|
|
|
|
def color(text, color, bold=false)
|
|
|
|
return text unless colorize_logging
|
|
|
|
color = self.class.const_get(color.to_s.upcase) if color.is_a?(Symbol)
|
|
|
|
bold = bold ? BOLD : ""
|
|
|
|
"#{bold}#{color}#{text}#{CLEAR}"
|
|
|
|
end
|
|
|
|
end
|
2010-04-28 00:16:06 -04:00
|
|
|
end
|