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"]
|
level = job_hash["log_level"]
|
||||||
if level
|
if level
|
||||||
@logger.log_at(level) do
|
@logger.log_at(level) do
|
||||||
@logger.with_context(job_hash_context(job_hash), &block)
|
Sidekiq::Context.with(job_hash_context(job_hash), &block)
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
@logger.with_context(job_hash_context(job_hash), &block)
|
Sidekiq::Context.with(job_hash_context(job_hash), &block)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ module Sidekiq
|
||||||
end
|
end
|
||||||
|
|
||||||
def with_elapsed_time_context(start, &block)
|
def with_elapsed_time_context(start, &block)
|
||||||
@logger.with_context(elapsed_time_context(start), &block)
|
Sidekiq::Context.with(elapsed_time_context(start), &block)
|
||||||
end
|
end
|
||||||
|
|
||||||
def elapsed_time_context(start)
|
def elapsed_time_context(start)
|
||||||
|
|
|
@ -4,18 +4,20 @@ require "logger"
|
||||||
require "time"
|
require "time"
|
||||||
|
|
||||||
module Sidekiq
|
module Sidekiq
|
||||||
module LoggingUtils
|
module Context
|
||||||
def with_context(hash)
|
def self.with(hash)
|
||||||
ctx.merge!(hash)
|
current.merge!(hash)
|
||||||
yield
|
yield
|
||||||
ensure
|
ensure
|
||||||
hash.each_key { |key| ctx.delete(key) }
|
hash.each_key { |key| current.delete(key) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def ctx
|
def self.current
|
||||||
Thread.current[:sidekiq_context] ||= {}
|
Thread.current[:sidekiq_context] ||= {}
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
module LoggingUtils
|
||||||
LEVELS = {
|
LEVELS = {
|
||||||
"debug" => 0,
|
"debug" => 0,
|
||||||
"info" => 1,
|
"info" => 1,
|
||||||
|
@ -114,7 +116,7 @@ module Sidekiq
|
||||||
end
|
end
|
||||||
|
|
||||||
def ctx
|
def ctx
|
||||||
Thread.current[:sidekiq_context] ||= {}
|
Sidekiq::Context.current
|
||||||
end
|
end
|
||||||
|
|
||||||
def format_context
|
def format_context
|
||||||
|
|
|
@ -39,31 +39,31 @@ class TestLogger < Minitest::Test
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_with_context
|
def test_with_context
|
||||||
subject = @logger
|
subject = Sidekiq::Context
|
||||||
assert_equal({}, subject.ctx)
|
assert_equal({}, subject.current)
|
||||||
|
|
||||||
subject.with_context(a: 1) do
|
subject.with(a: 1) do
|
||||||
assert_equal({ a: 1 }, subject.ctx)
|
assert_equal({ a: 1 }, subject.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal({}, subject.ctx)
|
assert_equal({}, subject.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_nested_contexts
|
def test_nested_contexts
|
||||||
subject = @logger
|
subject = Sidekiq::Context
|
||||||
assert_equal({}, subject.ctx)
|
assert_equal({}, subject.current)
|
||||||
|
|
||||||
subject.with_context(a: 1) do
|
subject.with(a: 1) do
|
||||||
assert_equal({ a: 1 }, subject.ctx)
|
assert_equal({ a: 1 }, subject.current)
|
||||||
|
|
||||||
subject.with_context(b: 2, c: 3) do
|
subject.with(b: 2, c: 3) do
|
||||||
assert_equal({ a: 1, b: 2, c: 3 }, subject.ctx)
|
assert_equal({ a: 1, b: 2, c: 3 }, subject.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal({ a: 1 }, subject.ctx)
|
assert_equal({ a: 1 }, subject.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
assert_equal({}, subject.ctx)
|
assert_equal({}, subject.current)
|
||||||
end
|
end
|
||||||
|
|
||||||
def test_formatted_output
|
def test_formatted_output
|
||||||
|
@ -76,7 +76,7 @@ class TestLogger < Minitest::Test
|
||||||
Sidekiq::Logger::Formatters::JSON, ]
|
Sidekiq::Logger::Formatters::JSON, ]
|
||||||
formats.each do |fmt|
|
formats.each do |fmt|
|
||||||
@logger.formatter = fmt.new
|
@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")
|
@logger.info("hello context")
|
||||||
end
|
end
|
||||||
assert_match(/INFO/, @output.string)
|
assert_match(/INFO/, @output.string)
|
||||||
|
@ -90,7 +90,7 @@ class TestLogger < Minitest::Test
|
||||||
@logger.formatter = Sidekiq::Logger::Formatters::JSON.new
|
@logger.formatter = Sidekiq::Logger::Formatters::JSON.new
|
||||||
|
|
||||||
@logger.debug("boom")
|
@logger.debug("boom")
|
||||||
@logger.with_context(class: 'HaikuWorker', jid: '1234abc') do
|
Sidekiq::Context.with(class: 'HaikuWorker', jid: '1234abc') do
|
||||||
@logger.info("json format")
|
@logger.info("json format")
|
||||||
end
|
end
|
||||||
a, b = @output.string.lines
|
a, b = @output.string.lines
|
||||||
|
|
Loading…
Add table
Reference in a new issue