2018-12-28 18:05:51 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative 'helper'
|
|
|
|
require 'sidekiq/job_logger'
|
|
|
|
|
|
|
|
class TestJobLogger < Minitest::Test
|
2018-12-29 10:54:05 -05:00
|
|
|
def setup
|
|
|
|
@old = Sidekiq.logger
|
|
|
|
@output = StringIO.new
|
2019-09-25 07:46:09 -04:00
|
|
|
@logger = Sidekiq::Logger.new(@output, level: :info)
|
|
|
|
Sidekiq.logger = @logger
|
2018-12-28 18:05:51 -05:00
|
|
|
|
2018-12-29 10:54:05 -05:00
|
|
|
Thread.current[:sidekiq_context] = nil
|
|
|
|
Thread.current[:sidekiq_tid] = nil
|
|
|
|
end
|
2018-12-28 18:05:51 -05:00
|
|
|
|
2018-12-29 10:54:05 -05:00
|
|
|
def teardown
|
|
|
|
Thread.current[:sidekiq_context] = nil
|
|
|
|
Thread.current[:sidekiq_tid] = nil
|
2019-09-25 07:46:09 -04:00
|
|
|
Sidekiq.logger = @old
|
2018-12-29 10:54:05 -05:00
|
|
|
end
|
2018-12-28 18:05:51 -05:00
|
|
|
|
2018-12-29 10:54:05 -05:00
|
|
|
def test_pretty_output
|
|
|
|
jl = Sidekiq::JobLogger.new(@logger)
|
2018-12-28 18:05:51 -05:00
|
|
|
|
2018-12-29 10:54:05 -05:00
|
|
|
# pretty
|
|
|
|
p = @logger.formatter = Sidekiq::Logger::Formatters::Pretty.new
|
2019-09-21 17:03:59 -04:00
|
|
|
job = {"jid"=>"1234abc", "wrapped"=>"FooWorker", "class"=>"Wrapper", "tags" => ["bar", "baz"]}
|
2018-12-29 10:54:05 -05:00
|
|
|
# this mocks what Processor does
|
2019-09-25 18:53:04 -04:00
|
|
|
jl.prepare(job) do
|
2018-12-29 10:54:05 -05:00
|
|
|
jl.call(job, 'queue') {}
|
|
|
|
end
|
2018-12-28 18:05:51 -05:00
|
|
|
|
2018-12-29 10:54:05 -05:00
|
|
|
a, b = @output.string.lines
|
|
|
|
assert a
|
|
|
|
assert b
|
2018-12-28 18:05:51 -05:00
|
|
|
|
2019-09-21 17:03:59 -04:00
|
|
|
expected = /pid=#{$$} tid=#{p.tid} class=FooWorker jid=1234abc tags=bar,baz/
|
2018-12-29 10:54:05 -05:00
|
|
|
assert_match(expected, a)
|
|
|
|
assert_match(expected, b)
|
|
|
|
assert_match(/#{Time.now.utc.to_date}.+Z pid=#{$$} tid=#{p.tid} .+INFO: done/, b)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_json_output
|
|
|
|
# json
|
|
|
|
@logger.formatter = Sidekiq::Logger::Formatters::JSON.new
|
|
|
|
jl = Sidekiq::JobLogger.new(@logger)
|
2019-09-21 17:03:59 -04:00
|
|
|
job = {"jid"=>"1234abc", "wrapped"=>"Wrapper", "class"=>"FooWorker", "bid"=>"b-xyz", "tags" => ["bar", "baz"]}
|
2018-12-29 10:54:05 -05:00
|
|
|
# this mocks what Processor does
|
2019-09-25 18:53:04 -04:00
|
|
|
jl.prepare(job) do
|
2018-12-29 10:54:05 -05:00
|
|
|
jl.call(job, 'queue') {}
|
2018-12-28 18:05:51 -05:00
|
|
|
end
|
2018-12-29 10:54:05 -05:00
|
|
|
a, b = @output.string.lines
|
|
|
|
assert a
|
|
|
|
assert b
|
|
|
|
hsh = JSON.parse(a)
|
|
|
|
keys = hsh.keys.sort
|
|
|
|
assert_equal(["ctx", "lvl", "msg", "pid", "tid", "ts"], keys)
|
|
|
|
keys = hsh["ctx"].keys.sort
|
2019-09-21 17:03:59 -04:00
|
|
|
assert_equal(["bid", "class", "jid", "tags"], keys)
|
2018-12-29 10:54:05 -05:00
|
|
|
end
|
|
|
|
|
2019-09-25 07:46:09 -04:00
|
|
|
def test_custom_log_level
|
|
|
|
jl = Sidekiq::JobLogger.new(@logger)
|
|
|
|
job = {"class"=>"FooWorker", "log_level"=>"debug"}
|
|
|
|
|
|
|
|
assert @logger.info?
|
2019-09-25 18:53:04 -04:00
|
|
|
jl.prepare(job) do
|
2019-09-25 07:46:09 -04:00
|
|
|
jl.call(job, "queue") do
|
|
|
|
assert @logger.debug?
|
|
|
|
@logger.debug("debug message")
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert @logger.info?
|
|
|
|
|
|
|
|
a, b, c = @output.string.lines
|
|
|
|
assert_match(/INFO: start/, a)
|
|
|
|
assert_match(/DEBUG: debug message/, b)
|
|
|
|
assert_match(/INFO: done/, c)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_custom_log_level_uses_default_log_level_for_invalid_value
|
|
|
|
jl = Sidekiq::JobLogger.new(@logger)
|
|
|
|
job = {"class"=>"FooWorker", "log_level"=>"non_existent"}
|
|
|
|
|
|
|
|
assert @logger.info?
|
2019-09-25 18:53:04 -04:00
|
|
|
jl.prepare(job) do
|
2019-09-25 07:46:09 -04:00
|
|
|
jl.call(job, "queue") do
|
|
|
|
assert @logger.info?
|
|
|
|
end
|
|
|
|
end
|
|
|
|
assert @logger.info?
|
|
|
|
log_level_warning = @output.string.lines[0]
|
2019-09-25 18:52:48 -04:00
|
|
|
assert_match(/WARN: Invalid log level/, log_level_warning)
|
2019-09-25 07:46:09 -04:00
|
|
|
end
|
|
|
|
|
2018-12-29 10:54:05 -05:00
|
|
|
def reset(io)
|
|
|
|
io.truncate(0)
|
|
|
|
io.rewind
|
2018-12-28 18:05:51 -05:00
|
|
|
end
|
|
|
|
end
|