跳至内容 跳至搜索

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"
 
BOLD = ActiveSupport::Deprecation::DeprecatedObjectProxy.new("\e[1m", "BOLD 已弃用!请改用 MODES[:bold]。", ActiveSupport.deprecator)
 
CLEAR = ActiveSupport::Deprecation::DeprecatedObjectProxy.new("\e[0m", "CLEAR 已弃用!请改用 MODES[:clear]。", ActiveSupport.deprecator)
 

嵌入到 String 中以清除所有先前的 ANSI 序列。

CYAN = "\e[36m"
 
绿色 = "\e[32m"
 
级别检查 = { debug: -> (logger) { !logger.debug? }, info: -> (logger) { !logger.info? }, error: -> (logger) { !logger.error? }, }
 
洋红色 = "\e[35m"
 
模式 = { clear: 0, bold: 1, italic: 3, underline: 4, }
 

ANSI 序列模式

红色 = "\e[31m"
 
白色 = "\e[37m"
 
黄色 = "\e[33m"
 

属性

[W] logger

类公共方法

flush_all!()

刷新所有 log_subscribers 的记录器。

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

log_subscribers()

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

logger()

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

new()

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

实例公共方法

call(event)

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

logger()

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

publish_event(event)

# File activesupport/lib/active_support/log_subscriber.rb, line 155
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 145
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 175
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