how to make rails+unicorn logger thread safe?

走远了吗. 提交于 2020-01-13 19:12:27

问题


We've been using unicorn to deploy our application. Everything went fine except for the production.log file, which turned out to be unreadable because the way unicorn was designed. Every instance of unicorn wrote to the same file, causing all the lines spaghetti'ed together.

So is there a way to tell the logger to log independently across multiple unicorn instances?


回答1:


edit your unicorn.conf.rb, and change the after_fork block to something like:

after_fork do |server, worker|

  filepath = "#{Rails.root}/log/#{Rails.env}.#{worker.nr}.log"
  Rails.logger = Logger.new(filepath, File::WRONLY | File::APPEND)
  ActiveSupport::LogSubscriber.logger = Rails.logger
  ActionController::Base.logger = Rails.logger
  ActionMailer::Base.logger = Rails.logger
  ActiveResource::Base.logger = Rails.logger

end


来源:https://stackoverflow.com/questions/10767478/how-to-make-railsunicorn-logger-thread-safe

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!