Active Support 日志订阅器¶ ↑
ActiveSupport::LogSubscriber 是一个用于消耗 ActiveSupport::Notifications 并专门用于记录它们的类。日志订阅器根据其给定的命名空间将通知分派给注册的对象。
例如,Active Record 日志订阅器负责记录 SQL 查询。
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 提供了一个简单的接口来检查被 instrumented 的代码是否抛出了异常。通常,在发生错误时会记录一条不同的消息,这可以通过扩展前面的示例来实现。
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
- 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" |
Attributes
| [W] | logger |
类公共方法
flush_all!() 链接
刷新所有 log_subscribers 的 logger。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/log_subscriber.rb, line 112 def flush_all! logger.flush if logger.respond_to?(:flush) end
log_subscribers() 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/log_subscriber.rb, line 107 def log_subscribers subscribers end
logger() 链接
来源: 显示 | 在 GitHub 上
# 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() 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/log_subscriber.rb, line 133 def initialize super @event_levels = {} end
实例公共方法
call(event) 链接
来源: 显示 | 在 GitHub 上
# 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() 链接
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/log_subscriber.rb, line 138 def logger LogSubscriber.logger end
silenced?(event) 链接
来源: 显示 | 在 GitHub 上
# 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 末尾的格式。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/log_subscriber.rb, line 166 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