diff options
Diffstat (limited to 'activesupport/lib/active_support/tagged_logging.rb')
-rw-r--r-- | activesupport/lib/active_support/tagged_logging.rb | 81 |
1 files changed, 38 insertions, 43 deletions
diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb index dc3ca25938..f6ad861353 100644 --- a/activesupport/lib/active_support/tagged_logging.rb +++ b/activesupport/lib/active_support/tagged_logging.rb @@ -1,63 +1,58 @@ require 'active_support/core_ext/object/blank' require 'logger' +require 'active_support/logger' module ActiveSupport - # Wraps any standard Logger class to provide tagging capabilities. Examples: + # Wraps any standard Logger object 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" + # 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. - class TaggedLogging - def initialize(logger) - @logger = logger - @tags = Hash.new { |h,k| h[k] = [] } - end - - def tagged(*new_tags) - tags = current_tags - new_tags = Array(new_tags).flatten.reject(&:blank?) - tags.concat new_tags - yield - ensure - new_tags.size.times { tags.pop } - end + module TaggedLogging + class Formatter < ActiveSupport::Logger::SimpleFormatter # :nodoc: + # This method is invoked when a log event occurs + def call(severity, timestamp, progname, msg) + super(severity, timestamp, progname, "#{tags_text}#{msg}") + end - def add(severity, message = nil, progname = nil, &block) - @logger.add(severity, "#{tags_text}#{message}", progname, &block) - end + def clear! + current_tags.clear + end - %w( fatal error warn info debug unknown ).each do |severity| - eval <<-EOM, nil, __FILE__, __LINE__ + 1 - def #{severity}(progname = nil, &block) # def warn(progname = nil, &block) - add(Logger::#{severity.upcase}, progname, &block) # add(Logger::WARN, progname, &block) - end # end - EOM - end + def current_tags + Thread.current[:activesupport_tagged_logging_tags] ||= [] + end - def flush - @tags.delete(Thread.current) - @logger.flush if @logger.respond_to?(:flush) + private + def tags_text + tags = current_tags + if tags.any? + tags.collect { |tag| "[#{tag}] " }.join + end + end end - def method_missing(method, *args) - @logger.send(method, *args) + def self.new(logger) + logger.formatter = Formatter.new + logger.extend(self) end - protected - - def tags_text - tags = current_tags - if tags.any? - tags.collect { |tag| "[#{tag}]" }.join(" ") + " " - end + def tagged(*new_tags) + tags = formatter.current_tags + new_tags = new_tags.flatten.reject(&:blank?) + tags.concat new_tags + yield + ensure + tags.pop(new_tags.size) end - def current_tags - @tags[Thread.current] + def flush + formatter.clear! + super if defined?(super) end end end |