1
0
Fork 0
mirror of https://github.com/mperham/sidekiq.git synced 2022-11-09 13:52:34 -05:00

Merge pull request #2110 from grosser/grosser/logging

fix done not logging when using with_context in a worker
This commit is contained in:
Mike Perham 2014-12-30 14:03:45 -08:00
commit 1130a30aad
2 changed files with 42 additions and 7 deletions

View file

@ -5,6 +5,8 @@ module Sidekiq
module Logging
class Pretty < Logger::Formatter
SPACE = " "
# Provide a call() method that returns the formatted message.
def call(severity, time, program_name, message)
"#{time.utc.iso8601(3)} #{::Process.pid} TID-#{Thread.current.object_id.to_s(36)}#{context} #{severity}: #{message}\n"
@ -12,17 +14,16 @@ module Sidekiq
def context
c = Thread.current[:sidekiq_context]
c ? " #{c}" : ''
" #{c.join(SPACE)}" if c && c.any?
end
end
def self.with_context(msg)
begin
Thread.current[:sidekiq_context] = msg
Thread.current[:sidekiq_context] ||= []
Thread.current[:sidekiq_context] << msg
yield
ensure
Thread.current[:sidekiq_context] = nil
end
Thread.current[:sidekiq_context].pop
end
def self.initialize_logger(log_target = STDOUT)

34
test/test_logging.rb Normal file
View file

@ -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 nil
end
it "can add a context" do
Sidekiq::Logging.with_context "xx" do
context.must_equal " xx"
end
context.must_equal nil
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 " xx yy"
end
context.must_equal " xx"
end
context.must_equal nil
end
end
end
end