2010-06-24 07:23:43 -04:00
|
|
|
require 'active_support/core_ext/module/attribute_accessors'
|
|
|
|
require 'active_support/core_ext/class/attribute'
|
2013-04-15 09:23:46 -04:00
|
|
|
require 'active_support/subscriber'
|
2010-01-12 06:52:57 -05:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
module ActiveSupport
|
2012-09-17 01:22:18 -04:00
|
|
|
# ActiveSupport::LogSubscriber is an object set to consume
|
|
|
|
# ActiveSupport::Notifications 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
|
|
|
#
|
2012-09-17 01:22:18 -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
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# Since we need to know all instance methods before attaching the log
|
|
|
|
# subscriber, 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.
|
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# Log subscriber also has some helpers to deal with logging and automatically
|
|
|
|
# flushes all logs when the request finishes (via action_dispatch.callback
|
|
|
|
# notification) in a Rails environment.
|
2013-04-15 09:23:46 -04:00
|
|
|
class LogSubscriber < Subscriber
|
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 << self
|
|
|
|
def logger
|
2013-04-03 13:19:30 -04:00
|
|
|
@logger ||= if defined?(Rails) && Rails.respond_to?(:logger)
|
|
|
|
Rails.logger
|
2013-01-09 18:33:32 -05:00
|
|
|
end
|
2010-01-12 06:52:57 -05:00
|
|
|
end
|
2010-01-14 16:03:47 -05:00
|
|
|
|
2012-06-19 14:56:27 -04:00
|
|
|
attr_writer :logger
|
|
|
|
|
2010-08-15 07:41:01 -04:00
|
|
|
def log_subscribers
|
2013-04-15 09:23:46 -04:00
|
|
|
subscribers
|
2010-06-04 12:48:29 -04:00
|
|
|
end
|
|
|
|
|
2010-08-15 07:41:01 -04:00
|
|
|
# Flush all log_subscribers' logger.
|
|
|
|
def flush_all!
|
2012-06-20 17:21:23 -04:00
|
|
|
logger.flush if logger.respond_to?(:flush)
|
2010-08-15 07:41:01 -04:00
|
|
|
end
|
2010-01-12 06:52:57 -05:00
|
|
|
end
|
|
|
|
|
2012-06-19 14:56:27 -04:00
|
|
|
def logger
|
|
|
|
LogSubscriber.logger
|
|
|
|
end
|
|
|
|
|
2012-06-18 19:34:23 -04:00
|
|
|
def start(name, id, payload)
|
2013-04-15 09:23:46 -04:00
|
|
|
super if logger
|
2012-06-18 19:34:23 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def finish(name, id, payload)
|
2013-04-15 09:23:46 -04:00
|
|
|
super if logger
|
|
|
|
rescue Exception => e
|
|
|
|
logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}"
|
2010-07-18 20:37:39 -04:00
|
|
|
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
|
2012-01-20 16:36:18 -05:00
|
|
|
def #{level}(progname = nil, &block)
|
2012-03-15 23:54:39 -04:00
|
|
|
logger.#{level}(progname, &block) if logger
|
2010-01-12 06:52:57 -05:00
|
|
|
end
|
|
|
|
METHOD
|
|
|
|
end
|
|
|
|
|
|
|
|
# Set color by using a string or one of the defined constants. If a third
|
2012-09-17 01:22:18 -04:00
|
|
|
# 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
|
2012-06-04 16:24:55 -04:00
|
|
|
color = self.class.const_get(color.upcase) if color.is_a?(Symbol)
|
2010-01-12 06:52:57 -05:00
|
|
|
bold = bold ? BOLD : ""
|
|
|
|
"#{bold}#{color}#{text}#{CLEAR}"
|
|
|
|
end
|
|
|
|
end
|
2010-04-28 00:16:06 -04:00
|
|
|
end
|