记录 Rails 应用程序中的所有方法调用
有没有一种简单的方法来记录 Rails 应用程序中的所有方法调用?
我对此的主要用途是测试(以及调试测试)。我希望拥有比堆栈跟踪提供的更多历史记录(例如,使用“-b”选项运行 rspec 时)。
Is there an easy way to log all method calls in a Rails app?
My main use for this would be in testing (and in debugging tests). I want to have more of a history than a stacktrace provides (for instance, when running rspec with the '-b' option).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这很容易做到。只需将 5 行代码添加到您的脚本/服务器中:
它在 http://phrogz.net/ProgrammingRuby/ospace.html 中进行了描述#tracingyourprogramsexecution
您的应用程序将变得非常慢,并且您可能会得到比您想要的更多的输出。您可以轻松地在文件/类/函数名称上添加更多条件,以避免打印不需要的内容。
It's easy to do. Just add 5 lines of code into your script/server:
It's described at http://phrogz.net/ProgrammingRuby/ospace.html#tracingyourprogramsexecution
Your application will become quite slow and you might get more output than you want. You can easily add more conditions on file/class/function names to avoid printing unwanted stuff.
Perftools 可能会为您提供所需的内容。它分析整个过程,并可以为您提供一个图形视图,看起来像这样 。 Rack perftools profiler 是一个使用 perftools 的 rubygem,可以轻松地与 Rails 应用程序集成,因此如果您想尝试的话,我建议您这样做。
Perftools might give you what you're looking for. It analyzes the entire process and can give you a graphical view that looks something like this. Rack perftools profiler is a rubygem that uses perftools and makes it easy to integrate with a Rails application, so I would recommend going with that if you want to try it.
首先,堆栈跟踪是发生错误时堆栈上的每个方法调用,除此之外您还想要什么其他历史记录?
其次,回答你的问题,没有简单的方法来记录所有方法调用。您可以一路提高日志级别进行调试,这应该会在日志中提供更多内容,但这只是某人实际选择记录的内容,与方法调用无关。
以这样的方式修补 ruby 可能并不困难,即每个方法调用都会在方法执行之前和之后打印一些日志语句,但这将再次类似于堆栈跟踪无论如何都会给您提供的内容,并且可能更少因为你不会得到行号等。
如果你想要比堆栈跟踪更多的信息,日志记录是大多数人会做的方式。
Firstly stacktrace IS every method call that was on the stack at the time an error occurred, what other history could you want besides this?
Secondly, to answer your question, no there is no easy way to log all method calls. You could up your log level all the way to debug which should give you more stuff in the logs, but this will only be things that someone has actually chosen to log, unrelated to method calls.
It probably wouldn't be that difficult to patch ruby in such a way that every method call will print some log statements before and after the method execution, but this will once again be similar to what a stack trace would give you anyway and potentially less since you won't get line numbers etc.
If you want more info than the stack trace, logging is the way most people would do it.