diff options
author | José Valim <jose.valim@gmail.com> | 2011-10-19 22:26:56 +0200 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2011-10-19 22:26:56 +0200 |
commit | 6c126015a676c376f1646713fbb739049a783238 (patch) | |
tree | eab5e9279c9710b308ebdc8998856ff397866d92 /activesupport/lib/active_support | |
parent | 4ef74536940ea4c8c7f8c2cb0252bfe5f0db6fdf (diff) | |
download | rails-6c126015a676c376f1646713fbb739049a783238.tar.gz rails-6c126015a676c376f1646713fbb739049a783238.tar.bz2 rails-6c126015a676c376f1646713fbb739049a783238.zip |
Ensure TaggegLogging is thread safe.
Diffstat (limited to 'activesupport/lib/active_support')
-rw-r--r-- | activesupport/lib/active_support/tagged_logging.rb | 35 |
1 files changed, 22 insertions, 13 deletions
diff --git a/activesupport/lib/active_support/tagged_logging.rb b/activesupport/lib/active_support/tagged_logging.rb index 0cabb528ef..aff416a9eb 100644 --- a/activesupport/lib/active_support/tagged_logging.rb +++ b/activesupport/lib/active_support/tagged_logging.rb @@ -13,20 +13,20 @@ module ActiveSupport class TaggedLogging def initialize(logger) @logger = logger - @tags = [] + @tags = Hash.new { |h,k| h[k] = [] } end - def tagged(*tags) - new_tags = Array.wrap(tags).flatten - @tags += new_tags + def tagged(*new_tags) + tags = current_tags + new_tags = Array.wrap(new_tags).flatten + tags.concat new_tags yield ensure - new_tags.size.times { @tags.pop } + new_tags.size.times { tags.pop } end - def add(severity, message = nil, progname = nil, &block) - @logger.add(severity, "#{tags}#{message}", progname, &block) + @logger.add(severity, "#{tags_text}#{message}", progname, &block) end %w( fatal error warn info debug unkown ).each do |severity| @@ -37,17 +37,26 @@ module ActiveSupport EOM end + def flush(*args) + @tags.delete(Thread.current) + @logger.flush(*args) if @logger.respond_to?(:flush) + end def method_missing(method, *args) @logger.send(method, *args) end - - private - def tags - if @tags.any? - @tags.collect { |tag| "[#{tag}]" }.join(" ") + " " - end + protected + + def tags_text + tags = current_tags + if tags.any? + tags.collect { |tag| "[#{tag}]" }.join(" ") + " " end + end + + def current_tags + @tags[Thread.current] + end end end |