Parent

Class/Module Index [+]

Quicksearch

ActiveSupport::TaggedLogging

Wraps any standard Logger class to provide tagging capabilities. Examples:

Logger = ActiveSupport::TaggedLogging.new(Logger.new(STDOUT))
Logger.tagged("BCX") { Logger.info "Stuff" }                            # Logs "[BCX] Stuff"
Logger.tagged("BCX", "Jason") { Logger.info "Stuff" }                   # Logs "[BCX] [Jason] Stuff"
Logger.tagged("BCX") { Logger.tagged("Jason") { Logger.info "Stuff" } } # Logs "[BCX] [Jason] Stuff"

This is used by the default Rails.logger as configured by Railties to make it easy to stamp log lines with subdomains, request ids, and anything else to aid debugging of multi-user production applications.

Public Class Methods

new(logger) click to toggle source
# File lib/active_support/tagged_logging.rb, line 16
def initialize(logger)
  @logger = logger
  @tags   = Hash.new { |h,k| h[k] = [] }
end

Public Instance Methods

add(severity, message = nil, progname = nil, &block) click to toggle source
# File lib/active_support/tagged_logging.rb, line 35
def add(severity, message = nil, progname = nil, &block)
  message = (block_given? ? block.call : progname) if message.nil?
  @logger.add(severity, "#{tags_text}#{message}", progname)
end
flush() click to toggle source
# File lib/active_support/tagged_logging.rb, line 48
def flush
  @tags.delete(Thread.current)
  @logger.flush if @logger.respond_to?(:flush)
end
method_missing(method, *args) click to toggle source
# File lib/active_support/tagged_logging.rb, line 53
def method_missing(method, *args)
  @logger.send(method, *args)
end
silence(temporary_level = Logger::ERROR, &block) click to toggle source
# File lib/active_support/tagged_logging.rb, line 30
def silence(temporary_level = Logger::ERROR, &block)
  @logger.silence(temporary_level, &block)
end
tagged(*new_tags) click to toggle source
# File lib/active_support/tagged_logging.rb, line 21
def tagged(*new_tags)
  tags     = current_tags
  new_tags = Array.wrap(new_tags).flatten.reject(&:blank?)
  tags.concat new_tags
  yield
ensure
  new_tags.size.times { tags.pop }
end

Protected Instance Methods

current_tags() click to toggle source
# File lib/active_support/tagged_logging.rb, line 66
def current_tags
  @tags[Thread.current]
end
tags_text() click to toggle source
# File lib/active_support/tagged_logging.rb, line 59
def tags_text
  tags = current_tags
  if tags.any?
    tags.collect { |tag| "[#{tag}]" }.join(" ") + " "
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.