diff --git a/judoscale-ruby/lib/judoscale/config.rb b/judoscale-ruby/lib/judoscale/config.rb index 5fc27d51..d923f718 100644 --- a/judoscale-ruby/lib/judoscale/config.rb +++ b/judoscale-ruby/lib/judoscale/config.rb @@ -110,7 +110,7 @@ def reset end def log_level=(new_level) - @log_level = new_level ? ::Logger::Severity.const_get(new_level.to_s.upcase) : nil + @log_level = get_severity_log_level(new_level) end def as_json @@ -127,5 +127,21 @@ def as_json def ignore_large_requests? @max_request_size_bytes end + + private + + def get_severity_log_level(log_level) + return nil if log_level.to_s.strip.empty? + + upcased_log_level = log_level.to_s.upcase + + if ::Logger::Severity.const_defined?(upcased_log_level) + ::Logger::Severity.const_get(upcased_log_level) + else + logger.warn "Invalid log_level detected: #{log_level}" + + nil + end + end end end diff --git a/judoscale-ruby/test/config_test.rb b/judoscale-ruby/test/config_test.rb index ad4c34ba..4b9d7ad1 100644 --- a/judoscale-ruby/test/config_test.rb +++ b/judoscale-ruby/test/config_test.rb @@ -89,6 +89,19 @@ module Judoscale end end + it "gracefully handles invalid JUDOSCALE_LOG_LEVEL values" do + env = { + "JUDOSCALE_LOG_LEVEL" => "not_a_real_log_level" + } + + use_env env do + config = Config.instance + _(config.log_level).must_be_nil + end + + _(log_string).must_include "Invalid log_level detected: not_a_real_log_level" + end + it "supports legacy ENV var configs" do env = { "RAILS_AUTOSCALE_MAX_QUEUES" => "42", @@ -133,6 +146,28 @@ module Judoscale _(enabled_adapter_configs).must_be :empty? end + it "gracefully handles invalid log_level values and logs a warning" do + Judoscale.configure do |config| + config.log_level = :not_a_real_log_level + end + + config = Config.instance + _(config.log_level).must_be_nil + + _(log_string).must_include "Invalid log_level detected: not_a_real_log_level" + end + + it "gracefully handles blank log_level values" do + Judoscale.configure do |config| + config.log_level = " " + end + + config = Config.instance + _(config.log_level).must_be_nil + + _(log_string).wont_include "Invalid log_level detected" + end + it "dumps the configuration options as json" do _(Config.instance.as_json).must_equal({ log_level: nil,