Created
June 6, 2014 09:23
-
-
Save YanhaoYang/5437b66ee6e9a1e21d6a to your computer and use it in GitHub Desktop.
Rails itself writes tagged logs in Rails log files. Sidekiq workers can also write logs into Rails log files. But these logs are not tagged by default, therefore, it is hard to identify which logs are generated by a specific worker. With Sidekiq's middleware, it is quit easy to convert those logs into tagged logs.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Sidekiq | |
module Middleware | |
module Server | |
class TaggedLogger | |
def call(worker, item, queue) | |
tag = "#{worker.class.to_s} #{SecureRandom.hex(12)}" | |
::Rails.logger.tagged(tag) do | |
job_info = "Start at #{Time.now.to_default_s}: #{item.inspect}" | |
::Rails.logger.info(job_info) | |
yield | |
end | |
end | |
end | |
end | |
end | |
end | |
Sidekiq.configure_server do |config| | |
config.server_middleware do |chain| | |
chain.add Sidekiq::Middleware::Server::TaggedLogger | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment