Rails production.log(使用Passenger)被多个进程写入,无法解析

时间:2012-06-21 19:06:22

标签: ruby-on-rails ruby-on-rails-3 apache passenger

这是我的production.log的片段:

Started GET "/product/514034/754240" for XX.XX.202.138 at 2012-06-21 11:52:28 -0700


Started GET "/product/614409/666897" for XX.XX.228.38 at 2012-06-21 11:52:28 -0700
Processing by ProductsController#show as HTML
  Parameters: {"category_id"=>"514034", "product_id"=>"754240"}
Processing by ProductsController#show as HTML
  Parameters: {"category_id"=>"614409", "product_id"=>"666897"}
Logged in 2940659 via auth cookie
Logged in 585210 via auth cookie
[e3e3fc56bb6bd137741b269ee397683c] [2940659] Read fragment views/global-caches/header (0.7ms)
[e3e3fc56bb6bd137741b269ee397683c] [2940659]   Rendered shared/_email_form.html.haml (0.7ms)
[d81bb986be5acc0277c0c9e11b414249] [585210] Read fragment views/global-caches/sharebar-message (0.7ms)
[d81bb986be5acc0277c0c9e11b414249] [585210]   Rendered shared/_email_form.html.haml (0.7ms)
...

如您所见,它将两个不同用户的两个并发会话同时记录到同一个日志文件中。这使得无法解析我的日志并确定生成每种页面所花费的时间,因为条目不符合预期的顺序:

Started GET "/URL/BLAH" for IP at DATE
... stuff...
Completed 200 OK in XXms (ActiveRecord: YY.Yms)

相反,我得到了一个不可预测的交错日志:

Started GET "/URL/BLAH" for IP at DATE

Started GET "/URL/BLAH" for IP at DATE
... stuff...
Completed 200 OK in XXms (ActiveRecord: YY.Yms)
...stuff...
Completed 200 OK in XXms (ActiveRecord: YY.Yms)

所以不可能将“已完成”与“已开始”匹配。

我想要的是让每个子进程写入自己的日志或其他内容的方法。或者,如果有可能以原子方式编写每个网页视图的日志,但这可能是不可能的,也可能是困难的或损害性能。

1 个答案:

答案 0 :(得分:3)

Rails 3.2提供了很好的选项config.log_tag

您可以添加到production.rb:

config.log_tags = [ lambda { Time.now.to_i }]

因此,日志中的每一行都将以数字为前缀。例如:

  

[1351867173]在2012-11-02 16:39:33 +0200左右开始获取127.0.0.1的“/”   [1351867173] RecipesController#main作为HTML处理

日志仍然被洗牌,但现在我们可以规范化,订购它们。

  

sort -f -s -k1.1,1.11 production.log | sed's /^.............//'> sorted_production.log

(按第一个符号排序(按时间戳排序)并按sed删除时间戳)

现在日志很容易分析。

此外,还修复了rails 3.2.9中的相关问题https://github.com/rails/rails/pull/7317 所以请记住这一点 抱歉英语不好......)