如何使Rails + Unicorn记录器线程安全? [英] how to make rails+unicorn logger thread safe?

查看:82
本文介绍了如何使Rails + Unicorn记录器线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们一直在使用独角兽来部署我们的应用程序.除了production.log文件外,其他一切都正常,这是由于设计独角兽的方式而导致无法读取.独角兽的每个实例都写入相同的文件,从而使所有行都成为意大利面条.

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?

推荐答案

编辑您的unicorn.conf.rb,并将after_fork块更改为以下内容:

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

这篇关于如何使Rails + Unicorn记录器线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆