how to make rails+unicorn logger thread safe?

2019-05-26 19:23发布

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条回答
该账号已被封号
2楼-- · 2019-05-26 19:56

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
查看更多
登录 后发表回答