Active Support 標記記錄
包裝任何標準 Logger
物件以提供標記功能。
可以使用區塊呼叫
logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
logger.tagged('BCX') { logger.info 'Stuff' } # Logs "[BCX] Stuff"
logger.tagged('BCX', "Jason") { |tagged_logger| tagged_logger.info 'Stuff' } # Logs "[BCX] [Jason] Stuff"
logger.tagged('BCX') { logger.tagged('Jason') { logger.info 'Stuff' } } # Logs "[BCX] [Jason] Stuff"
如果在沒有區塊的情況下呼叫,將傳回套用標記的新記錄器
logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
logger.tagged("BCX").info "Stuff" # Logs "[BCX] Stuff"
logger.tagged("BCX", "Jason").info "Stuff" # Logs "[BCX] [Jason] Stuff"
logger.tagged("BCX").tagged("Jason").info "Stuff" # Logs "[BCX] [Jason] Stuff"
這是預設 Rails.logger
使用 Railties 設定的,這樣可以輕鬆地使用子網域、要求 ID 以及任何其他有助於偵錯多使用者製作應用程式的資料來標記記錄行。
方法
類別公開方法
new(logger) 連結
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/tagged_logging.rb, line 117 def self.new(logger) logger = logger.clone if logger.formatter logger.formatter = logger.formatter.clone else # Ensure we set a default formatter so we aren't extending nil! logger.formatter = ActiveSupport::Logger::SimpleFormatter.new end logger.formatter.extend Formatter logger.extend(self) end
執行個體公開方法
flush() 連結
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/tagged_logging.rb, line 144 def flush clear_tags! super if defined?(super) end
tagged(*tags) 連結
來源:顯示 | 在 GitHub 上
# File activesupport/lib/active_support/tagged_logging.rb, line 133 def tagged(*tags) if block_given? formatter.tagged(*tags) { yield self } else logger = ActiveSupport::TaggedLogging.new(self) logger.formatter.extend LocalTagStorage logger.push_tags(*formatter.current_tags, *tags) logger end end