2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2017-10-21 09:11:29 -04:00
|
|
|
require "active_support/core_ext/module/attribute_accessors"
|
|
|
|
require "active_support/core_ext/class/attribute"
|
|
|
|
require "active_support/subscriber"
|
2010-01-12 06:52:57 -05:00
|
|
|
|
2010-06-24 07:23:43 -04:00
|
|
|
module ActiveSupport
|
2018-12-14 16:59:44 -05:00
|
|
|
# <tt>ActiveSupport::LogSubscriber</tt> is an object set to consume
|
|
|
|
# <tt>ActiveSupport::Notifications</tt> with the sole purpose of logging them.
|
2012-09-17 01:22:18 -04:00
|
|
|
# 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)
|
2018-12-31 15:17:40 -05:00
|
|
|
# info "#{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
|
|
|
#
|
2018-12-14 16:59:44 -05:00
|
|
|
# After configured, whenever a <tt>"sql.active_record"</tt> notification is published,
|
|
|
|
# it will properly dispatch the event
|
|
|
|
# (<tt>ActiveSupport::Notifications::Event</tt>) to the sql method.
|
|
|
|
#
|
|
|
|
# Being an <tt>ActiveSupport::Notifications</tt> consumer,
|
|
|
|
# <tt>ActiveSupport::LogSubscriber</tt> exposes a simple interface to check if
|
|
|
|
# instrumented code raises an exception. It is common to log a different
|
|
|
|
# message in case of an error, and this can be achieved by extending
|
|
|
|
# the previous example:
|
|
|
|
#
|
|
|
|
# module ActiveRecord
|
|
|
|
# class LogSubscriber < ActiveSupport::LogSubscriber
|
|
|
|
# def sql(event)
|
|
|
|
# exception = event.payload[:exception]
|
|
|
|
#
|
|
|
|
# if exception
|
|
|
|
# exception_object = event.payload[:exception_object]
|
|
|
|
#
|
2018-12-31 15:17:40 -05:00
|
|
|
# error "[ERROR] #{event.payload[:name]}: #{exception.join(', ')} " \
|
|
|
|
# "(#{exception_object.backtrace.first})"
|
2018-12-14 16:59:44 -05:00
|
|
|
# else
|
|
|
|
# # standard logger code
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
|
|
|
# end
|
2010-01-12 06:52:57 -05:00
|
|
|
#
|
2012-09-17 01:22:18 -04:00
|
|
|
# Log subscriber also has some helpers to deal with logging and automatically
|
2018-12-14 16:59:44 -05:00
|
|
|
# flushes all logs when the request finishes
|
|
|
|
# (via <tt>action_dispatch.callback</tt> 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
|
|
|
|
2017-05-31 05:16:20 -04:00
|
|
|
mattr_accessor :colorize_logging, default: 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
|
2017-05-18 19:06:56 -04:00
|
|
|
rescue => e
|
2017-05-18 19:07:35 -04:00
|
|
|
if logger
|
|
|
|
logger.error "Could not log #{name.inspect} event. #{e.class}: #{e.message} #{e.backtrace}"
|
|
|
|
end
|
2010-07-18 20:37:39 -04:00
|
|
|
end
|
|
|
|
|
2016-12-17 03:13:50 -05:00
|
|
|
private
|
2010-01-12 06:52:57 -05:00
|
|
|
|
|
|
|
%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
|
|
|
|
|
2015-07-17 16:23:59 -04:00
|
|
|
# Set color by using a symbol 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.
|
2016-12-17 03:13:50 -05:00
|
|
|
def color(text, color, bold = false) # :doc:
|
2010-01-12 06:52:57 -05:00
|
|
|
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
|