How to split up rails 3.x logger by unicorn workers

config/unicorn/staging.conf

worker_processes 4

after_fork do |server, worker|
  $worker_nr = worker.nr                                                                                                     
end

config/application.rb

module MyApp
  class Application < Rails::Application
    #... lots of config options
    config.logger = ActiveSupport::TaggedLogging.new(Logger.new(Rails.root.join("log", "#{Rails.env}_#{$worker_nr}.log").to_s))
  end
end
Bonyiii Over 9 years ago