Skip to content

Commit

Permalink
Add config knob, :skip_default_job_logging, to disable logging out of…
Browse files Browse the repository at this point in the history
… the box, see #6199 (#6200)
  • Loading branch information
mperham committed May 8, 2024
1 parent 3c2d7b8 commit 6677b45
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 15 deletions.
33 changes: 23 additions & 10 deletions lib/sidekiq/job_logger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,36 @@

module Sidekiq
class JobLogger
def initialize(logger)
include Sidekiq::Component

def initialize(config)
@config = config
@logger = logger
end

# If true we won't do any job logging out of the box.
# The user is responsible for any logging.
def skip_default_logging?
config[:skip_default_job_logging]
end

def call(item, queue)
start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
@logger.info("start")
return yield if skip_default_logging?

yield
begin
start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
@logger.info("start")

Sidekiq::Context.add(:elapsed, elapsed(start))
@logger.info("done")
rescue Exception
Sidekiq::Context.add(:elapsed, elapsed(start))
@logger.info("fail")
yield

Sidekiq::Context.add(:elapsed, elapsed(start))
@logger.info("done")
rescue Exception
Sidekiq::Context.add(:elapsed, elapsed(start))
@logger.info("fail")

raise
raise
end
end

def prepare(job_hash, &block)
Expand Down
2 changes: 1 addition & 1 deletion lib/sidekiq/processor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def initialize(capsule, &block)
@job = nil
@thread = nil
@reloader = Sidekiq.default_configuration[:reloader]
@job_logger = (capsule.config[:job_logger] || Sidekiq::JobLogger).new(logger)
@job_logger = (capsule.config[:job_logger] || Sidekiq::JobLogger).new(capsule.config)
@retrier = Sidekiq::JobRetry.new(capsule)
end

Expand Down
24 changes: 20 additions & 4 deletions test/job_logger_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,24 @@
Thread.current[:sidekiq_tid] = nil
end

it "allows output to be disabled" do
@logger.formatter = Sidekiq::Logger::Formatters::Pretty.new
@cfg[:skip_default_job_logging] = true

jl = Sidekiq::JobLogger.new(@cfg)
@logger.info "mike"
job = {"jid" => "1234abc", "wrapped" => "FooJob", "class" => "Wrapper", "tags" => ["bar", "baz"]}
jl.prepare(job) do
jl.call(job, "queue") {}
end

a, b = @output.string.lines
assert_match(/mike/, a)
refute b
end

it "tests pretty output" do
jl = Sidekiq::JobLogger.new(@logger)
jl = Sidekiq::JobLogger.new(@cfg)

# pretty
p = @logger.formatter = Sidekiq::Logger::Formatters::Pretty.new
Expand All @@ -45,7 +61,7 @@
it "tests json output" do
# json
@logger.formatter = Sidekiq::Logger::Formatters::JSON.new
jl = Sidekiq::JobLogger.new(@logger)
jl = Sidekiq::JobLogger.new(@cfg)
job = {"jid" => "1234abc", "wrapped" => "Wrapper", "class" => "FooJob", "bid" => "b-xyz", "tags" => ["bar", "baz"]}
# this mocks what Processor does
jl.prepare(job) do
Expand All @@ -62,7 +78,7 @@
end

it "tests custom log level" do
jl = Sidekiq::JobLogger.new(@logger)
jl = Sidekiq::JobLogger.new(@cfg)
job = {"class" => "FooJob", "log_level" => "debug"}

assert @logger.info?
Expand All @@ -84,7 +100,7 @@
@logger = Logger.new(@output, level: :info)
@cfg.logger = @logger

jl = Sidekiq::JobLogger.new(@logger)
jl = Sidekiq::JobLogger.new(@cfg)
job = {"class" => "FooJob", "log_level" => "debug"}

assert @logger.info?
Expand Down

0 comments on commit 6677b45

Please sign in to comment.