diff options
author | Rafael França <rafaelmfranca@gmail.com> | 2015-12-23 17:37:04 -0200 |
---|---|---|
committer | Rafael França <rafaelmfranca@gmail.com> | 2015-12-23 17:37:04 -0200 |
commit | d6fe65a36d3b8f9943e38d70e7eba3e8ac28aed4 (patch) | |
tree | 4561f7a11ead2ada27aa080709e619c60979d1cd /activesupport/lib | |
parent | 08724cc1549dbe6fa3c02ef030b89b106c91a51c (diff) | |
parent | 629efb605728b31ad9644f6f0acaf3760b641a29 (diff) | |
download | rails-d6fe65a36d3b8f9943e38d70e7eba3e8ac28aed4.tar.gz rails-d6fe65a36d3b8f9943e38d70e7eba3e8ac28aed4.tar.bz2 rails-d6fe65a36d3b8f9943e38d70e7eba3e8ac28aed4.zip |
Merge pull request #20507 from rabbitt/bugfix/logger-threadsafety
Fixes LoggerSilence#silence threadsafety
Diffstat (limited to 'activesupport/lib')
-rw-r--r-- | activesupport/lib/active_support/logger.rb | 14 | ||||
-rw-r--r-- | activesupport/lib/active_support/logger_silence.rb | 24 |
2 files changed, 36 insertions, 2 deletions
diff --git a/activesupport/lib/active_support/logger.rb b/activesupport/lib/active_support/logger.rb index 82117a64d2..520268b244 100644 --- a/activesupport/lib/active_support/logger.rb +++ b/activesupport/lib/active_support/logger.rb @@ -46,6 +46,20 @@ module ActiveSupport super @formatter = SimpleFormatter.new @broadcast_messages = true + after_initialize if respond_to? :after_initialize + end + + def add(severity, message = nil, progname = nil, &block) + return true if @logdev.nil? || (severity || UNKNOWN) < level + super + end + + Logger::Severity.constants.each do |severity| + class_eval(<<-EOT, __FILE__, __LINE__ + 1) + def #{severity.downcase}? # def debug? + Logger::#{severity} >= level # DEBUG >= level + end # end + EOT end # Simple formatter which only displays the message. diff --git a/activesupport/lib/active_support/logger_silence.rb b/activesupport/lib/active_support/logger_silence.rb index 7d92256f24..690e5596f7 100644 --- a/activesupport/lib/active_support/logger_silence.rb +++ b/activesupport/lib/active_support/logger_silence.rb @@ -1,22 +1,42 @@ require 'active_support/concern' require 'active_support/core_ext/module/attribute_accessors' +require 'concurrent' module LoggerSilence extend ActiveSupport::Concern included do cattr_accessor :silencer + attr_reader :local_levels self.silencer = true end + def after_initialize + @local_levels = Concurrent::Map.new(:initial_capacity => 2) + end + + def local_log_id + Thread.current.__id__ + end + + def level + local_levels[local_log_id] || super + end + # Silences the logger for the duration of the block. def silence(temporary_level = Logger::ERROR) if silencer begin - old_logger_level, self.level = level, temporary_level + old_local_level = local_levels[local_log_id] + local_levels[local_log_id] = temporary_level + yield self ensure - self.level = old_logger_level + if old_local_level + local_levels[local_log_id] = old_local_level + else + local_levels.delete(local_log_id) + end end else yield self |