Class: Sidekiq::JobLogger

Inherits:
Object
  • Object
show all
Includes:
Component
Defined in:
lib/sidekiq/job_logger.rb

Instance Attribute Summary

Attributes included from Component

#config

Instance Method Summary collapse

Methods included from Component

#fire_event, #handle_exception, #hostname, #identity, #logger, #process_nonce, #redis, #safe_thread, #tid, #watchdog

Constructor Details

#initialize(config) ⇒ JobLogger

Returns a new instance of JobLogger.



7
8
9
10
# File 'lib/sidekiq/job_logger.rb', line 7

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

Instance Method Details

#call(item, queue) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/sidekiq/job_logger.rb', line 18

def call(item, queue)
  return yield if skip_default_logging?

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

    yield

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

    raise
  end
end

#prepare(job_hash, &block) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/sidekiq/job_logger.rb', line 37

def prepare(job_hash, &block)
  # If we're using a wrapper class, like ActiveJob, use the "wrapped"
  # attribute to expose the underlying thing.
  h = {
    class: job_hash["display_class"] || job_hash["wrapped"] || job_hash["class"],
    jid: job_hash["jid"]
  }
  h[:bid] = job_hash["bid"] if job_hash.has_key?("bid")
  h[:tags] = job_hash["tags"] if job_hash.has_key?("tags")

  Thread.current[:sidekiq_context] = h
  level = job_hash["log_level"]
  if level && @logger.respond_to?(:log_at)
    @logger.log_at(level, &block)
  else
    yield
  end
ensure
  Thread.current[:sidekiq_context] = nil
end

#skip_default_logging?Boolean

If true we won’t do any job logging out of the box. The user is responsible for any logging.

Returns:

  • (Boolean)


14
15
16
# File 'lib/sidekiq/job_logger.rb', line 14

def skip_default_logging?
  config[:skip_default_job_logging]
end