1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Improve compatibility with Semantic Logger (#5238)

Semantic Logger returns Symbol for `#level`:
```
[1] pry(main)> SemanticLogger[Sidekiq].level
=> :debug
```
so current code fails with
```
[2] pry(main)> Sidekiq.logger = SemanticLogger[Sidekiq]
=> #<SemanticLogger::Logger:0x00007f1d27984a18 @filter=nil, @level=nil, @level_index=nil, @name="Sidekiq">
[3] pry(main)> Sidekiq.logger.debug?
ArgumentError: comparison of Symbol with 0 failed
from /home/ojab/.rbenv/versions/3.1.1/lib/ruby/gems/3.1.0/gems/sidekiq-6.4.1/lib/sidekiq/logger.rb:39:in
`<='
```
Calling `#{level}?` methods on the logger directly instead of
comparing `#level` with integer fixes that.
This commit is contained in:
ojab 2022-03-10 20:20:18 +03:00 committed by GitHub
parent 70da442444
commit 20cdf68a73
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 38 additions and 20 deletions

View file

@ -204,12 +204,12 @@ module Sidekiq
end
def self.logger
@logger ||= Sidekiq::Logger.new($stdout, level: Logger::INFO)
@logger ||= Sidekiq::Logger.new($stdout, level: :info)
end
def self.logger=(logger)
if logger.nil?
self.logger.level = Logger::FATAL
self.logger.fatal!
return self.logger
end

View file

@ -35,24 +35,10 @@ module Sidekiq
nil
end
def debug?
level <= 0
end
def info?
level <= 1
end
def warn?
level <= 2
end
def error?
level <= 3
end
def fatal?
level <= 4
LEVELS.each do |level, numeric_level|
define_method("#{level}?") do
local_level.nil? ? super() : local_level <= numeric_level
end
end
def local_level

View file

@ -93,4 +93,36 @@ class TestJobLogger < Minitest::Test
log_level_warning = @output.string.lines[0]
assert_match(/WARN: Invalid log level/, log_level_warning)
end
def test_custom_logger_with_non_numeric_levels
logger_class = Class.new(Logger) do
def level
:nonsense
end
def info?
true
end
def debug?
false
end
end
@logger = logger_class.new(@output, level: :info)
Sidekiq.logger = @logger
jl = Sidekiq::JobLogger.new(@logger)
job = {"class" => "FooWorker", "log_level" => "debug"}
assert @logger.info?
refute @logger.debug?
jl.prepare(job) do
jl.call(job, "queue") do
assert @logger.debug?
end
end
assert @logger.info?
refute @logger.debug?
end
end