LoginSignup
9
9

More than 5 years have passed since last update.

Unicorn worker毎にRailsログを別ファイルに書き出す

Posted at

Unicornで複数のRailsプロセスを動かしていると同じproduction.logにログが書きだされます。その結果ログの中身が混ざって後からみたとき何がなにやら訳がわからない感じになります。ログは人力で見るものじゃないという声もあるかと思いますが、解析器に書けたときの揺れも大きくなってしまいます。ワーカー数が増えてくると尚更辛い感じです。

分割する

config/unicorn.rb の after_fork の中でこんな感じに書いておくとワーカー毎にログが別々に出力されるようになります。

config/unicorn.rb
after_fork do |server, worker|
  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 = Rails.configuration.autoflush_log
    logger = ActiveSupport::Logger.new f
    logger.formatter = Rails.configuration.log_formatter
    logger = ActiveSupport::TaggedLogging.new(logger)
    logger
  end
end

参考リンク

9
9
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
9
9