rails_semantic_logger icon indicating copy to clipboard operation
rails_semantic_logger copied to clipboard

Include sidekiq job class in log tags?

Open jdelStrother opened this issue 10 months ago • 0 comments
trafficstars

Environment

Provide at least:

  • Ruby Version. 3.3
  • Rails Version. 7.2
  • Semantic Logger Version. 4.16
  • Rails Semantic Logger Version. 4.17
  • Sidekiq 7.3.7

Expected Behavior

Sidekiq's default job logger tags the logs with the job class:

2025-01-10T10:23:27.148Z pid=47763 tid=15bv class=SimpleWorker jid=38c1f5938b17233b4e0b842b INFO: start
2025-01-10T10:23:27.160Z pid=47763 tid=15bv class=SimpleWorker jid=38c1f5938b17233b4e0b842b INFO: Hello world
2025-01-10T10:23:27.216Z pid=47763 tid=15bv class=SimpleWorker jid=38c1f5938b17233b4e0b842b INFO: rails logger says hi
2025-01-10T10:23:27.217Z pid=47763 tid=15bv class=SimpleWorker jid=38c1f5938b17233b4e0b842b elapsed=0.069 INFO: done

Actual Behavior

RailsSemanticLogger only tags it with the jid by default:

2025-01-10 10:28:59.471449 I [48258:sidekiq.default/processor] {jid: f82aa891af96cbda43fc2981, queue: default} SimpleWorker -- Start #perform
2025-01-10 10:28:59.479035 I [48258:sidekiq.default/processor] {jid: f82aa891af96cbda43fc2981, queue: default} SimpleWorker -- Hello world
2025-01-10 10:28:59.523899 I [48258:sidekiq.default/processor] {jid: f82aa891af96cbda43fc2981, queue: default} Rails -- rails logger says hi
2025-01-10 10:28:59.525212 I [48258:sidekiq.default/processor] {jid: f82aa891af96cbda43fc2981, queue: default} (53.7ms) SimpleWorker -- Completed #perform

which can make it hard to keep track of which job is responsible for some nested log statement coming from ActiveRecord or whatever.

Pull requests

For now I'm monkeypatching it:

RailsSemanticLogger::Sidekiq::JobLogger.class_eval do
  def job_hash_context(job_hash)
    h         = { jid: job_hash["jid"], class: job_hash["wrapped"] || job_hash["class"] }
    h[:bid]   = job_hash["bid"] if job_hash["bid"]
    h[:tags]  = job_hash["tags"] if job_hash["tags"]
    h[:queue] = job_hash["queue"] if job_hash["queue"]
    h
  end
end

but I can submit a PR if this seems like a good idea

jdelStrother avatar Jan 10 '25 11:01 jdelStrother