mirror of
https://github.com/mperham/sidekiq.git
synced 2022-11-09 13:52:34 -05:00
refactor log context to not require context functionality on the logger itself, #4296
This commit is contained in:
parent
5e3a57e87e
commit
daabec1c7b
3 changed files with 26 additions and 24 deletions
|
@ -27,10 +27,10 @@ module Sidekiq
|
|||
level = job_hash["log_level"]
|
||||
if level
|
||||
@logger.log_at(level) do
|
||||
@logger.with_context(job_hash_context(job_hash), &block)
|
||||
Sidekiq::Context.with(job_hash_context(job_hash), &block)
|
||||
end
|
||||
else
|
||||
@logger.with_context(job_hash_context(job_hash), &block)
|
||||
Sidekiq::Context.with(job_hash_context(job_hash), &block)
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -47,7 +47,7 @@ module Sidekiq
|
|||
end
|
||||
|
||||
def with_elapsed_time_context(start, &block)
|
||||
@logger.with_context(elapsed_time_context(start), &block)
|
||||
Sidekiq::Context.with(elapsed_time_context(start), &block)
|
||||
end
|
||||
|
||||
def elapsed_time_context(start)
|
||||
|
|
|
@ -4,18 +4,20 @@ require "logger"
|
|||
require "time"
|
||||
|
||||
module Sidekiq
|
||||
module LoggingUtils
|
||||
def with_context(hash)
|
||||
ctx.merge!(hash)
|
||||
module Context
|
||||
def self.with(hash)
|
||||
current.merge!(hash)
|
||||
yield
|
||||
ensure
|
||||
hash.each_key { |key| ctx.delete(key) }
|
||||
hash.each_key { |key| current.delete(key) }
|
||||
end
|
||||
|
||||
def ctx
|
||||
def self.current
|
||||
Thread.current[:sidekiq_context] ||= {}
|
||||
end
|
||||
end
|
||||
|
||||
module LoggingUtils
|
||||
LEVELS = {
|
||||
"debug" => 0,
|
||||
"info" => 1,
|
||||
|
@ -114,7 +116,7 @@ module Sidekiq
|
|||
end
|
||||
|
||||
def ctx
|
||||
Thread.current[:sidekiq_context] ||= {}
|
||||
Sidekiq::Context.current
|
||||
end
|
||||
|
||||
def format_context
|
||||
|
|
|
@ -39,31 +39,31 @@ class TestLogger < Minitest::Test
|
|||
end
|
||||
|
||||
def test_with_context
|
||||
subject = @logger
|
||||
assert_equal({}, subject.ctx)
|
||||
subject = Sidekiq::Context
|
||||
assert_equal({}, subject.current)
|
||||
|
||||
subject.with_context(a: 1) do
|
||||
assert_equal({ a: 1 }, subject.ctx)
|
||||
subject.with(a: 1) do
|
||||
assert_equal({ a: 1 }, subject.current)
|
||||
end
|
||||
|
||||
assert_equal({}, subject.ctx)
|
||||
assert_equal({}, subject.current)
|
||||
end
|
||||
|
||||
def test_nested_contexts
|
||||
subject = @logger
|
||||
assert_equal({}, subject.ctx)
|
||||
subject = Sidekiq::Context
|
||||
assert_equal({}, subject.current)
|
||||
|
||||
subject.with_context(a: 1) do
|
||||
assert_equal({ a: 1 }, subject.ctx)
|
||||
subject.with(a: 1) do
|
||||
assert_equal({ a: 1 }, subject.current)
|
||||
|
||||
subject.with_context(b: 2, c: 3) do
|
||||
assert_equal({ a: 1, b: 2, c: 3 }, subject.ctx)
|
||||
subject.with(b: 2, c: 3) do
|
||||
assert_equal({ a: 1, b: 2, c: 3 }, subject.current)
|
||||
end
|
||||
|
||||
assert_equal({ a: 1 }, subject.ctx)
|
||||
assert_equal({ a: 1 }, subject.current)
|
||||
end
|
||||
|
||||
assert_equal({}, subject.ctx)
|
||||
assert_equal({}, subject.current)
|
||||
end
|
||||
|
||||
def test_formatted_output
|
||||
|
@ -76,7 +76,7 @@ class TestLogger < Minitest::Test
|
|||
Sidekiq::Logger::Formatters::JSON, ]
|
||||
formats.each do |fmt|
|
||||
@logger.formatter = fmt.new
|
||||
@logger.with_context(class: 'HaikuWorker', bid: 'b-1234abc') do
|
||||
Sidekiq::Context.with(class: 'HaikuWorker', bid: 'b-1234abc') do
|
||||
@logger.info("hello context")
|
||||
end
|
||||
assert_match(/INFO/, @output.string)
|
||||
|
@ -90,7 +90,7 @@ class TestLogger < Minitest::Test
|
|||
@logger.formatter = Sidekiq::Logger::Formatters::JSON.new
|
||||
|
||||
@logger.debug("boom")
|
||||
@logger.with_context(class: 'HaikuWorker', jid: '1234abc') do
|
||||
Sidekiq::Context.with(class: 'HaikuWorker', jid: '1234abc') do
|
||||
@logger.info("json format")
|
||||
end
|
||||
a, b = @output.string.lines
|
||||
|
|
Loading…
Reference in a new issue