Class: Wurk::JobLogger
- Inherits:
-
Object
- Object
- Wurk::JobLogger
- Defined in:
- lib/wurk/job_logger.rb
Overview
Wraps the per-job execution span. Logs "start"/"done"/"fail" at INFO, pushes :elapsed into Wurk::Context so the logger formatter can pick it up, and prepares the thread-local context hash (jid, class, plus config).
Two entry points, called from Processor#dispatch in this order:
1. prepare(job_hash) { ... } → sets thread-local context, applies
per-job log_level, yields to the rest of dispatch.
2. call(item, queue) { ... } → wraps the actual perform with the
start/done/fail log line trio and the elapsed-seconds measurement.
Skipping default logging is controlled by config; the prepare step still runs so context/log_level still apply.
Spec: docs/target/sidekiq-free.md §18.
Instance Method Summary collapse
- #call(_item, _queue) ⇒ Object
-
#initialize(config) ⇒ JobLogger
constructor
A new instance of JobLogger.
-
#prepare(job_hash, &block) ⇒ Object
Sets thread-local context for the duration of
block, optionally under a per-job log level.
Constructor Details
#initialize(config) ⇒ JobLogger
Returns a new instance of JobLogger.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/wurk/job_logger.rb', line 22 def initialize(config) @config = config @logger = @config.logger # Read once: `config[]` is a Hash lookup through a delegating Capsule, and # #context_hash runs it per job only to walk a list that cannot change # after boot (Configuration freezes at launch). @logged_attributes = Array(@config[:logged_job_attributes]).map { |attr| [attr, attr.to_sym] }.freeze # A host logger need not be a ::Logger >= 1.6 (SemanticLogger, a # broadcast wrapper): without #with_level a per-job `log_level` is # ignored rather than failing the job it was meant to annotate. @with_level = @logger.respond_to?(:with_level) @skip = @config[:skip_default_job_logging] ? true : false # Only consulted when `args` is a logged attribute — the one place this # logger prints arguments. See Wurk::Redact. @redact = @logged_attributes.any? { |attr, _| attr == 'args' } ? @config[:redact_args] : nil end |
Instance Method Details
#call(_item, _queue) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/wurk/job_logger.rb', line 39 def call(_item, _queue) start = ::Process.clock_gettime(::Process::CLOCK_MONOTONIC) @logger.info { 'start' } unless @skip yield Wurk::Context.add(:elapsed, elapsed(start)) @logger.info { 'done' } unless @skip rescue Exception # rubocop:disable Lint/RescueException Wurk::Context.add(:elapsed, elapsed(start)) @logger.info { 'fail' } unless @skip raise end |
#prepare(job_hash, &block) ⇒ Object
Sets thread-local context for the duration of block, optionally
under a per-job log level. ActiveJob-wrapped jobs expose the real
class via the "wrapped" key — log that, not the wrapper.
56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/wurk/job_logger.rb', line 56 def prepare(job_hash, &block) h = context_hash(job_hash) level = job_hash['log_level'] Wurk::Context.with(h) do if level && @with_level @logger.with_level(level, &block) else yield end end end |