How to make each unicorn worker of my Rails application log to a different file?

后端 未结 2 1285
情深已故
情深已故 2020-12-24 09:37

How can I make each unicorn worker of my Rails application writting in a different log file ?

The why : problem of mixed log files... In its default configuration, R

2条回答
  •  半阙折子戏
    2020-12-24 10:12

    @slact's answer doesn't work on Rails 3. This works:

    after_fork do |server, worker|
    
      # Override the default logger to use a separate log for each Unicorn worker.
      # https://github.com/rails/rails/blob/3-2-stable/railties/lib/rails/application/bootstrap.rb#L23-L49
      Rails.logger = ActiveRecord::Base.logger = ActionController::Base.logger = begin
        path = Rails.configuration.paths["log"].first
        f = File.open(path.sub(".log", "-#{worker.nr}.log"), "a")
        f.binmode
        f.sync = true
        logger = ActiveSupport::TaggedLogging.new(ActiveSupport::BufferedLogger.new(f))
        logger.level = ActiveSupport::BufferedLogger.const_get(Rails.configuration.log_level.to_s.upcase)
        logger
      end
    end
    

提交回复
热议问题