Class: Sidekiq::JobLogger

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

Instance Method Summary collapse

Constructor Details

#initialize(logger = Sidekiq.logger) ⇒ JobLogger

Returns a new instance of JobLogger.



5
6
7
# File 'lib/sidekiq/job_logger.rb', line 5

def initialize(logger = Sidekiq.logger)
  @logger = logger
end

Instance Method Details

#call(item, queue) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/sidekiq/job_logger.rb', line 9

def call(item, queue)
  start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC)
  @logger.info("start")

  yield

  with_elapsed_time_context(start) do
    @logger.info("done")
  end
rescue Exception
  with_elapsed_time_context(start) do
    @logger.info("fail")
  end

  raise
end

#elapsed_time_context(start) ⇒ Object



53
54
55
# File 'lib/sidekiq/job_logger.rb', line 53

def elapsed_time_context(start)
  {elapsed: elapsed(start).to_s}
end

#job_hash_context(job_hash) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
# File 'lib/sidekiq/job_logger.rb', line 37

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

#prepare(job_hash, &block) ⇒ Object



26
27
28
29
30
31
32
33
34
35
# File 'lib/sidekiq/job_logger.rb', line 26

def prepare(job_hash, &block)
  level = job_hash["log_level"]
  if level
    @logger.log_at(level) do
      Sidekiq::Context.with(job_hash_context(job_hash), &block)
    end
  else
    Sidekiq::Context.with(job_hash_context(job_hash), &block)
  end
end

#with_elapsed_time_context(start, &block) ⇒ Object



49
50
51
# File 'lib/sidekiq/job_logger.rb', line 49

def with_elapsed_time_context(start, &block)
  Sidekiq::Context.with(elapsed_time_context(start), &block)
end