Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Logging all method calls in a Rails app

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).

like image 804
dafmetal Avatar asked Aug 13 '11 14:08

dafmetal


2 Answers

It's easy to do. Just add 5 lines of code into your script/server:

#!/usr/bin/env ruby
set_trace_func proc {
  |event, file, line, id, binding, classname| 
  if event == "call" or event == "return" 
    printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
  end
}

require File.expand_path('../../config/boot',  __FILE__)
require 'commands/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.

like image 135
Uwe Geuder Avatar answered Sep 30 '22 18:09

Uwe Geuder


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.

like image 30
Peter Brown Avatar answered Sep 30 '22 18:09

Peter Brown