From fcdceadca1554c0a634c7851e86e81181107ea08 Mon Sep 17 00:00:00 2001 From: Adam McCrea Date: Fri, 8 Dec 2023 12:01:53 -0500 Subject: [PATCH] Use the configured log level even if the logger has already been initialized Fixes #187 Our own Railtie code makes calls to `logger` which might be run before a user's custom `Judoscale.configure` block. We need to allow configuring the log level at any time. --- judoscale-ruby/lib/judoscale/logger.rb | 6 +++++- judoscale-ruby/test/logger_test.rb | 9 +++++++++ 2 files changed, 14 insertions(+), 1 deletion(-) diff --git a/judoscale-ruby/lib/judoscale/logger.rb b/judoscale-ruby/lib/judoscale/logger.rb index 492a329b..dfc9ea2c 100644 --- a/judoscale-ruby/lib/judoscale/logger.rb +++ b/judoscale-ruby/lib/judoscale/logger.rb @@ -6,7 +6,11 @@ module Judoscale module Logger def logger - @logger ||= LoggerProxy.new(Config.instance.logger, Config.instance.log_level) + if @logger && @logger.log_level == Config.instance.log_level + @logger + else + @logger = LoggerProxy.new(Config.instance.logger, Config.instance.log_level) + end end end diff --git a/judoscale-ruby/test/logger_test.rb b/judoscale-ruby/test/logger_test.rb index 9f739ee9..955483f8 100644 --- a/judoscale-ruby/test/logger_test.rb +++ b/judoscale-ruby/test/logger_test.rb @@ -46,6 +46,15 @@ module Judoscale end end + it "respects the configured log_level even if the logger has been initialized" do + logger.debug "this triggers the logger initialization" + + use_config log_level: :fatal do + logger.public_send method_name, "some message" + _(messages).wont_include "some message" + end + end + it "respects the level set by the original logger when the log level config is not overridden" do original_logger.level = ::Logger::Severity::FATAL