diff --git a/console.gemspec b/console.gemspec index e50c59c..78cb1dd 100644 --- a/console.gemspec +++ b/console.gemspec @@ -20,6 +20,6 @@ Gem::Specification.new do |spec| spec.required_ruby_version = ">= 3.0" spec.add_dependency "fiber-annotation" - spec.add_dependency "fiber-local" + spec.add_dependency "fiber-storage" spec.add_dependency "json" end diff --git a/lib/console/logger.rb b/lib/console/logger.rb index d60d157..be171f5 100644 --- a/lib/console/logger.rb +++ b/lib/console/logger.rb @@ -13,12 +13,10 @@ require_relative 'terminal/logger' require_relative 'serialized/logger' -require 'fiber/local' +require 'fiber/storage' module Console class Logger < Filter[debug: 0, info: 1, warn: 2, error: 3, fatal: 4] - extend Fiber::Local - # Set the default log level based on `$DEBUG` and `$VERBOSE`. # You can also specify CONSOLE_LEVEL=debug or CONSOLE_LEVEL=info in environment. # https://mislav.net/2011/06/ruby-verbose-mode/ has more details about how it all fits together. @@ -56,8 +54,12 @@ def self.default_logger(output = $stderr, env = ENV, **options) return logger end - def self.local - self.default_logger + def self.instance + Fiber[:console_logger] ||= self.default_logger + end + + def self.instance=(logger) + Fiber[:console_logger] = logger end DEFAULT_LEVEL = 1 diff --git a/test/console/logger.rb b/test/console/logger.rb index 1b2b27f..1b96082 100644 --- a/test/console/logger.rb +++ b/test/console/logger.rb @@ -15,6 +15,22 @@ let(:message) {"Hello World"} + with '.instance' do + it "propagates to child thread" do + Fiber.new do + expect(Fiber[:console_logger]).to be_nil + + logger = Console::Logger.instance + + Fiber.new do + expect(Console::Logger.instance).to be_equal(logger) + end.resume + end.resume + + expect(Fiber[:console_logger]).to be_nil + end + end + with '#with' do let(:nested) {logger.with(name: "nested", level: :debug)}