Skip to content

Commit

Permalink
fix done not logging when using with_context in a worker
Browse files Browse the repository at this point in the history
  • Loading branch information
grosser committed Dec 30, 2014
1 parent 01dc468 commit 2d567ed
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 6 deletions.
10 changes: 4 additions & 6 deletions lib/sidekiq/logging.rb
Expand Up @@ -17,12 +17,10 @@ def context
end

def self.with_context(msg)
begin
Thread.current[:sidekiq_context] = msg
yield
ensure
Thread.current[:sidekiq_context] = nil
end
previous, Thread.current[:sidekiq_context] = Thread.current[:sidekiq_context], msg
yield
ensure
Thread.current[:sidekiq_context] = previous
end

def self.initialize_logger(log_target = STDOUT)
Expand Down
34 changes: 34 additions & 0 deletions test/test_logging.rb
@@ -0,0 +1,34 @@
require 'helper'
require 'sidekiq/logging'

class TestFetcher < Sidekiq::Test
describe Sidekiq::Logging do
describe "#with_context" do
def context
Sidekiq::Logging.logger.formatter.context
end

it "has no context by default" do
context.must_equal ""
end

it "can add a context" do
Sidekiq::Logging.with_context "xx" do
context.must_equal " xx"
end
context.must_equal ""
end

it "can use multiple contexts" do
Sidekiq::Logging.with_context "xx" do
context.must_equal " xx"
Sidekiq::Logging.with_context "yy" do
context.must_equal " yy"
end
context.must_equal " xx"
end
context.must_equal ""
end
end
end
end

0 comments on commit 2d567ed

Please sign in to comment.