aboutsummaryrefslogtreecommitdiffstats
path: root/activesupport/lib/active_support/tagged_logging.rb
diff options
context:
space:
mode:
authorAaron Patterson <aaron.patterson@gmail.com>2012-01-19 13:43:27 -0800
committerAaron Patterson <aaron.patterson@gmail.com>2012-01-19 13:44:00 -0800
commit8129a09589bce7f13ded9b465d29e16eaa1d86bf (patch)
treef3970727a8e477bad0e15a90f203ad40c487b201 /activesupport/lib/active_support/tagged_logging.rb
parent16bd0cb1f08569402cccc9b7f00f0b274e327eb9 (diff)
downloadrails-8129a09589bce7f13ded9b465d29e16eaa1d86bf.tar.gz
rails-8129a09589bce7f13ded9b465d29e16eaa1d86bf.tar.bz2
rails-8129a09589bce7f13ded9b465d29e16eaa1d86bf.zip
move tagged logging to a module, stop proxying every method call
Diffstat (limited to 'activesupport/lib/active_support/tagged_logging.rb')
-rw-r--r--activesupport/lib/active_support/tagged_logging.rb68
1 files changed, 32 insertions, 36 deletions
diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb
index b60bc94db4..af8cd6a646 100644
--- a/activesupport/lib/active_support/tagged_logging.rb
+++ b/activesupport/lib/active_support/tagged_logging.rb
@@ -1,5 +1,6 @@
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:
@@ -11,52 +12,47 @@ module ActiveSupport
#
# 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
+ 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 clear!
+ current_tags.clear
+ end
+
+ def current_tags
+ Thread.current[:activesupport_tagged_logging_tags] ||= []
+ end
+
+ private
+ def tags_text
+ tags = current_tags
+ if tags.any?
+ tags.collect { |tag| "[#{tag}] " }.join
+ end
+ end
+ end
+
+ def self.new(logger)
+ logger.formatter = Formatter.new
+ logger.extend(self)
end
def tagged(*new_tags)
- tags = current_tags
- new_tags = Array(new_tags).flatten.reject(&:blank?)
+ tags = formatter.current_tags
+ new_tags = new_tags.flatten.reject(&:blank?)
tags.concat new_tags
yield
ensure
tags.pop(new_tags.size)
end
- def add(severity, message = nil, progname = nil, &block)
- @logger.add(severity, "#{tags_text}#{message}", progname, &block)
- 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 flush
- current_tags.clear
- @logger.flush if @logger.respond_to?(:flush)
- end
-
- def method_missing(method, *args)
- @logger.send(method, *args)
- end
-
- protected
-
- def tags_text
- tags = current_tags
- if tags.any?
- tags.collect { |tag| "[#{tag}] " }.join
- end
- end
-
- def current_tags
- Thread.current[:activesupport_tagged_logging_tags] ||= []
+ formatter.clear!
+ super if defined?(super)
end
end
end