如何让我的Rails应用程序的每个独角兽工作者登录到另一个文件?

时间:2023-02-05 21:59:19

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

如何让我的Rails应用程序的每个独角兽工作者在另一个日志文件中写入?

The why : problem of mixed log files... In its default configuration, Rails will write its log messages to a single log file: log/<environment>.log.

原因:混合日志文件的问题...在其默认配置中,Rails会将其日志消息写入单个日志文件:log / .log。

Unicorn workers will write to the same log file at once, the messages can get mixed up. This is a problem when request-log-analyzer parses a log file. An example:

Unicorn工作人员会立即写入同一个日志文件,这些消息可能会混淆。当request-log-analyzer解析日志文件时,这是一个问题。一个例子:

Processing Controller1#action1 ...
Processing Controller2#action2 ...
Completed in 100ms...
Completed in 567ms...

In this example, what action was completed in 100ms, and what action in 567 ms? We can never be sure.

在这个例子中,在100ms内完成了什么动作,在567 ms内完成了什么动作?我们永远无法确定。

2 个解决方案

#1


3  

add this code to after_fork in unicorn.rb:

将此代码添加到unicorn.rb中的after_fork:

#one log per unicorn worker
if log = Rails.logger.instance_values['log']
  ext = File.extname log.path
  new_path =log.path.gsub %r{(.*)(#{Regexp.escape ext})}, "\\1.#{worker.nr}\\2"
  Rails.logger.instance_eval do
    @log.close
    @log= open_log new_path, 'a+'
  end
end

#2


2  

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

@ slact的答案在Rails 3上不起作用。这有效:

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

#1


3  

add this code to after_fork in unicorn.rb:

将此代码添加到unicorn.rb中的after_fork:

#one log per unicorn worker
if log = Rails.logger.instance_values['log']
  ext = File.extname log.path
  new_path =log.path.gsub %r{(.*)(#{Regexp.escape ext})}, "\\1.#{worker.nr}\\2"
  Rails.logger.instance_eval do
    @log.close
    @log= open_log new_path, 'a+'
  end
end

#2


2  

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

@ slact的答案在Rails 3上不起作用。这有效:

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