diff options
author | Rafael França <rafael@franca.dev> | 2019-07-24 16:28:56 -0400 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-07-24 16:28:56 -0400 |
commit | b1c27c059c88642aaf4a0c486320c03de4f165f6 (patch) | |
tree | e9d4ee89ef9ecbd6fdc9dbe27d68174fe0b26464 | |
parent | 96289cfb9b6aeb8f1a917f892148fd47f2f2049a (diff) | |
parent | 56ec504db6c130d448ffc1d68c9fdd95fdfc1130 (diff) | |
download | rails-b1c27c059c88642aaf4a0c486320c03de4f165f6.tar.gz rails-b1c27c059c88642aaf4a0c486320c03de4f165f6.tar.bz2 rails-b1c27c059c88642aaf4a0c486320c03de4f165f6.zip |
Merge pull request #36753 from cmrd-senya/36752-make-rails-logger-fiber-safe
Make ActiveSupport::Logger Fiber-safe
-rw-r--r-- | activesupport/CHANGELOG.md | 32 | ||||
-rw-r--r-- | activesupport/lib/active_support/logger_thread_safe_level.rb | 3 | ||||
-rw-r--r-- | activesupport/test/logger_test.rb | 44 |
3 files changed, 78 insertions, 1 deletions
diff --git a/activesupport/CHANGELOG.md b/activesupport/CHANGELOG.md index d56d4c22de..5d6bb7a0b3 100644 --- a/activesupport/CHANGELOG.md +++ b/activesupport/CHANGELOG.md @@ -1,3 +1,35 @@ +* Make ActiveSupport::Logger Fiber-safe. Fixes #36752. + + Use `Fiber.current.__id__` in `ActiveSupport::Logger#local_level=` in order + to make log level local to Ruby Fibers in addition to Threads. + + Example: + + logger = ActiveSupport::Logger.new(STDOUT) + logger.level = 1 + p "Main is debug? #{logger.debug?}" + + Fiber.new { + logger.local_level = 0 + p "Thread is debug? #{logger.debug?}" + }.resume + + p "Main is debug? #{logger.debug?}" + + Before: + + Main is debug? false + Thread is debug? true + Main is debug? true + + After: + + Main is debug? false + Thread is debug? true + Main is debug? false + + *Alexander Varnin* + * Allow the `on_rotation` proc used when decrypting/verifying a message to be passed at the constructor level. diff --git a/activesupport/lib/active_support/logger_thread_safe_level.rb b/activesupport/lib/active_support/logger_thread_safe_level.rb index f16c90cfc6..1775a41492 100644 --- a/activesupport/lib/active_support/logger_thread_safe_level.rb +++ b/activesupport/lib/active_support/logger_thread_safe_level.rb @@ -3,6 +3,7 @@ require "active_support/concern" require "active_support/core_ext/module/attribute_accessors" require "concurrent" +require "fiber" module ActiveSupport module LoggerThreadSafeLevel # :nodoc: @@ -28,7 +29,7 @@ module ActiveSupport end def local_log_id - Thread.current.__id__ + Fiber.current.__id__ end def local_level diff --git a/activesupport/test/logger_test.rb b/activesupport/test/logger_test.rb index 160e1156b6..6f7a186022 100644 --- a/activesupport/test/logger_test.rb +++ b/activesupport/test/logger_test.rb @@ -258,6 +258,50 @@ class LoggerTest < ActiveSupport::TestCase assert_level(Logger::INFO) end + def test_logger_level_main_fiber_safety + @logger.level = Logger::INFO + assert_level(Logger::INFO) + + fiber = Fiber.new do + assert_level(Logger::INFO) + end + + @logger.silence(Logger::ERROR) do + assert_level(Logger::ERROR) + fiber.resume + end + end + + def test_logger_level_local_fiber_safety + @logger.level = Logger::INFO + assert_level(Logger::INFO) + + another_fiber = Fiber.new do + @logger.silence(Logger::ERROR) do + assert_level(Logger::ERROR) + @logger.silence(Logger::DEBUG) do + assert_level(Logger::DEBUG) + end + end + + assert_level(Logger::INFO) + end + + Fiber.new do + @logger.silence(Logger::ERROR) do + assert_level(Logger::ERROR) + @logger.silence(Logger::DEBUG) do + another_fiber.resume + assert_level(Logger::DEBUG) + end + end + + assert_level(Logger::INFO) + end.resume + + assert_level(Logger::INFO) + end + private def level_name(level) ::Logger::Severity.constants.find do |severity| |