diff --git a/judoscale-ruby/lib/judoscale/config.rb b/judoscale-ruby/lib/judoscale/config.rb index 5fc27d51..4d1f378f 100644 --- a/judoscale-ruby/lib/judoscale/config.rb +++ b/judoscale-ruby/lib/judoscale/config.rb @@ -59,6 +59,10 @@ def as_json end end + BLANK_REGEXP = /\A[[:space:]]*\z/.freeze + + private_constant :BLANK_REGEXP + include Singleton @adapter_configs = [] @@ -110,7 +114,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 = blank_log_level?(new_level) ? nil : get_severity_log_level(new_level) end def as_json @@ -127,5 +131,23 @@ def as_json def ignore_large_requests? @max_request_size_bytes end + + private + + def blank_log_level?(log_level) + log_level.nil? || log_level.match?(BLANK_REGEXP) + end + + def get_severity_log_level(log_level) + 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..6d194c54 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,