问题描述
我回忆起整理得井井有条的日志文件,这样您就可以遵循一个请求,然后是下一个,依此类推.
I recollect getting log files that were nicely ordered, so that you could follow one request, then the next, and so on.
现在,就像我四岁的孩子所说的那样,日志文件全部被窃听了",这意味着它们不再是单独的,截然不同的文本块了.来自两个请求的日志相互交织/混合在一起.
Now, the log files are, as my 4 year old says "all scroggled up", meaning that they are no longer separate, distinct chunks of text. Loggings from two requests get intertwined/mixed up.
例如:
Started GET /foobar
...
Completed 200 OK in 2ms (Views: 0.4ms | ActiveRecord: 0.8ms)
Patient Load (wait, that's from another request that has nothing to do with foobar!)
[ blank space ]
Something else
这令人发疯,因为我无法确定单个请求中发生了什么.
This is maddening, because I can't tell what's happening within one single request.
这是在乘客上运行的.
推荐答案
是的,他们已经在ActiveSupport::BufferedLogger
中进行了一些更改,因此不再需要等到请求结束后才刷新日志:
Yep!, they have made some changes in the ActiveSupport::BufferedLogger
so it is not any more waiting until the request has ended to flush the logs:
- http://news.ycombinator.com/item?id=4483390
- https://github.com/rails/rails/commit/04ef93dae6d9cec616973c1110a33894ad4ba6ed
但是他们添加了 ActiveSupport :: TaggedLogging ,这很有趣并且您可以为每个日志加上所需标记的.
But they have added the ActiveSupport::TaggedLogging which is very funny and you can stamp every log with any kind of mark you want.
在您的情况下,最好对带有 request UUID 的日志进行 标记,如下所示:
In your case could be good to stamp the logs with the request UUID like this:
# config/application.rb
config.log_tags = [:uuid]
然后,即使日志被弄乱了,您仍然可以跟踪其中的哪些日志与您要跟踪的请求相对应.
Then even if the logs are messed up you still can follow which of them correspond to the request you are following up.
通过此功能,您可以做更多有趣的事情来帮助您进行日志研究:
You can make more funny things with this feature to help you in your logs study:
- How to log user_name in Rails?
- http://zogovic.com/post/21138929607/running-time-in-rails-logs
这篇关于Rails 3.2.2日志文件无序,请求交织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!