跳至內容 跳至搜尋

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::LogSubscriber 身為 ActiveSupport::Notifications 使用者,會公開一個簡單的介面,以檢查工具化程式碼是否引發例外。在發生錯誤時記錄不同的訊息很常見,這可以透過延伸前一個範例來達成

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

常數

黑色 = "\e[30m"
 

ANSI 序列顏色

藍色 = "\e[34m"
 
粗體 = ActiveSupport::Deprecation::DeprecatedObjectProxy.new("\e[1m", "粗體已棄用!改用 MODES[:bold]。", ActiveSupport.deprecator)
 
清除 = ActiveSupport::Deprecation::DeprecatedObjectProxy.new("\e[0m", "清除已棄用!改用 MODES[:clear]。", ActiveSupport.deprecator)
 

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

青色 = "\e[36m"
 
綠色 = "\e[32m"
 
LEVEL_CHECKS = { debug: -> (logger) { !logger.debug? }, info: -> (logger) { !logger.info? }, error: -> (logger) { !logger.error? }, }
 
洋紅色 = "\e[35m"
 
MODES = { 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 啟發,此方法會自動清除傳回的 字串 結尾的格式。

# 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