Log to different logger based on call from Sidekiq or Rails

前端 未结 1 2258
暖寄归人
暖寄归人 2021-02-20 15:32

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 t

1条回答
  •  被撕碎了的回忆
    2021-02-20 16:16

    Both Sidekiq and Rails use Logger compliant classes from the Ruby StdLib.

    You should be able to re-configure the Rails logger to use the Sidekiq logger in the Sidekiq server config block. Find/create a file like config/inititializers/sidekiq.rb:

    Sidekiq.configure_server do |config|
      Rails.logger = Sidekiq::Logging.logger
    
      # Sidekiq other server config
      ...
    end
    

    The block passed to the Sidekiq.configure_server call is only executed within the Server component of the Sidekiq backend, not within the client running in your Rails app server or console.

    0 讨论(0)
提交回复
热议问题