Skip to content

Commit

Permalink
Stop raising on unsupported log_level
Browse files Browse the repository at this point in the history
  • Loading branch information
lucaseras committed Oct 17, 2024
1 parent 079a361 commit 8bf484e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 1 deletion.
24 changes: 23 additions & 1 deletion judoscale-ruby/lib/judoscale/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ def as_json
end
end

BLANK_REGEXP = /\A[[:space:]]*\z/.freeze

private_constant :BLANK_REGEXP

include Singleton

@adapter_configs = []
Expand Down Expand Up @@ -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
Expand All @@ -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
35 changes: 35 additions & 0 deletions judoscale-ruby/test/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 8bf484e

Please sign in to comment.