I have an Rails 3.2 based app that uses Sidekiq 2.12 to run background jobs. The Sidekiq jobs can call the same methods as the interactive Rails app. I would like the methods to log to the Sidekiq log when called from Sidekiq and log to the Rails log when called from Rails. I am looking for a clean way to do this but so far have come up empty.
In both environments, both Sidekiq::Logging.logger
and Rails.logger
are defined and work.
- I do not want to inspect the call stack on every logging attempt in order to pick the right loggger.
- I do not want to pass a log object as a function parameter to every function that might be called from both Sidekiq and Rails.
I think probably the right thing do to is to have Sidekiq replace the Rails logger with its logger during the startup process and then always log to the Rails logger, but I can't find an initializer hook that runs after the Rails logger is set up but before Sidekiq jobs are started and is only run by Sidekiq.
Is there such a hook? If so, please tell me about it. If not, please provide other suggestions for how to cleanly redirect logging output based on the Sidekiq vs Rails running environment.