跳至内容 跳至搜索

Active Support 日志订阅者

ActiveSupport::LogSubscriber 是一个用于消费 ActiveSupport::Notifications 的对象,其唯一目的是记录这些通知。日志订阅者根据其给定的命名空间将通知分派到已注册的对象。

例如,Active Record 日志订阅者负责记录查询

module ActiveRecord
  class LogSubscriber < ActiveSupport::LogSubscriber
    attach_to :active_record

    def sql(event)
      info "#{event.payload[:name]} (#{event.duration}) #{event.payload[:sql]}"
    end
  end
end

ActiveRecord::LogSubscriber.logger 也必须设置,但在 Rails 环境中会自动分配。

配置后,每当发布 "sql.active_record" 通知时,它将正确地将事件 (ActiveSupport::Notifications::Event) 分派到 sql 方法。

作为 ActiveSupport::Notifications 消费者,ActiveSupport::LogSubscriber 提供了一个简单的接口来检查是否仪器代码引发了异常。通常情况下,会在发生错误时记录不同的消息,这可以通过扩展之前的示例来实现

module ActiveRecord
  class LogSubscriber < ActiveSupport::LogSubscriber
    def sql(event)
      exception = event.payload[:exception]

      if exception
        exception_object = event.payload[:exception_object]

        error "[ERROR] #{event.payload[:name]}: #{exception.join(', ')} " \
              "(#{exception_object.backtrace.first})"
      else
        # standard logger code
      end
    end
  end
end

ActiveSupport::LogSubscriber 还提供了一些帮助程序来处理日志记录。例如,ActiveSupport::LogSubscriber.flush_all! 将确保所有日志都已刷新,它在 Rails::Rack::Logger 中请求完成后调用。

命名空间
方法
C
F
L
N
P
S

常量

BLACK = "\e[30m"
 

ANSI 序列颜色

BLUE = "\e[34m"
 
CYAN = "\e[36m"
 
GREEN = "\e[32m"
 
LEVEL_CHECKS = { debug: -> (logger) { !logger.debug? }, info: -> (logger) { !logger.info? }, error: -> (logger) { !logger.error? }, }
 
MAGENTA = "\e[35m"
 
MODES = { clear: 0, bold: 1, italic: 3, underline: 4, }
 

ANSI 序列模式

RED = "\e[31m"
 
WHITE = "\e[37m"
 
YELLOW = "\e[33m"
 

属性

[W] logger

类公共方法

flush_all!()

刷新所有 log_subscribers 的日志记录器。

# File activesupport/lib/active_support/log_subscriber.rb, line 112
def flush_all!
  logger.flush if logger.respond_to?(:flush)
end

log_subscribers()

# File activesupport/lib/active_support/log_subscriber.rb, line 107
def log_subscribers
  subscribers
end

logger()

# File activesupport/lib/active_support/log_subscriber.rb, line 93
def logger
  @logger ||= if defined?(Rails) && Rails.respond_to?(:logger)
    Rails.logger
  end
end

new()

# File activesupport/lib/active_support/log_subscriber.rb, line 133
def initialize
  super
  @event_levels = {}
end

实例公共方法

call(event)

# File activesupport/lib/active_support/log_subscriber.rb, line 146
def call(event)
  super if logger
rescue => e
  log_exception(event.name, e)
end

logger()

# File activesupport/lib/active_support/log_subscriber.rb, line 138
def logger
  LogSubscriber.logger
end

publish_event(event)

# File activesupport/lib/active_support/log_subscriber.rb, line 152
def publish_event(event)
  super if logger
rescue => e
  log_exception(event.name, e)
end

silenced?(event)

# File activesupport/lib/active_support/log_subscriber.rb, line 142
def silenced?(event)
  logger.nil? || @event_levels[event]&.call(logger)
end

实例私有方法

color(text, color, mode_options = {})

使用符号或定义的常量之一设置颜色。通过指定粗体、斜体或下划线选项来设置模式。受 Highline 的启发,此方法将在返回的 String 结尾自动清除格式。

# File activesupport/lib/active_support/log_subscriber.rb, line 172
def color(text, color, mode_options = {}) # :doc:
  return text unless colorize_logging
  color = self.class.const_get(color.upcase) if color.is_a?(Symbol)
  mode = mode_from(mode_options)
  clear = "\e[#{MODES[:clear]}m"
  "#{mode}#{color}#{text}#{clear}"
end