From daabec1c7bc6fede5a11ff1dbde9193495eaf2c4 Mon Sep 17 00:00:00 2001 From: Mike Perham Date: Tue, 1 Oct 2019 19:19:26 -0700 Subject: [PATCH] refactor log context to not require context functionality on the logger itself, #4296 --- lib/sidekiq/job_logger.rb | 6 +++--- lib/sidekiq/logger.rb | 14 ++++++++------ test/test_logger.rb | 30 +++++++++++++++--------------- 3 files changed, 26 insertions(+), 24 deletions(-) diff --git a/lib/sidekiq/job_logger.rb b/lib/sidekiq/job_logger.rb index 7f24db6d..d6095037 100644 --- a/lib/sidekiq/job_logger.rb +++ b/lib/sidekiq/job_logger.rb @@ -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) diff --git a/lib/sidekiq/logger.rb b/lib/sidekiq/logger.rb index 49a4ae7f..5372ff35 100644 --- a/lib/sidekiq/logger.rb +++ b/lib/sidekiq/logger.rb @@ -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 diff --git a/test/test_logger.rb b/test/test_logger.rb index b6fca6d9..2fe65e4c 100644 --- a/test/test_logger.rb +++ b/test/test_logger.rb @@ -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