# Logging all method calls?

**URL:** <https://www.ruby-forum.com/t/logging-all-method-calls/114443>\
**Category:** Ruby\
**Created:** [September 12, 2007, 7:18pm UTC](https://www.ruby-forum.com/t/logging-all-method-calls/114443 "2007-09-12T19:18:40Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![s04015](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@s04015](https://www.ruby-forum.com/u/s04015)\
**Post date:** [September 12, 2007, 7:18pm UTC](https://www.ruby-forum.com/t/logging-all-method-calls/114443/1 "2007-09-12T19:18:40Z")

</div>

Hey

Is there a way to log all the method calls that occur throughout the  
execution of some script? Something like an hook that gets called  
whenever a  
method executes would be ideal.

Eg.

class Object  
def before\_method\_called(method\_name)  
p class.name + method\_name.to\_s  
end  
end

Thanks in advance,  
Gustav P.

---

<div class="post-metadata">

**Author:** ![s04015](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@s04015](https://www.ruby-forum.com/u/s04015)\
**Post date:** [September 12, 2007, 7:38pm UTC](https://www.ruby-forum.com/t/logging-all-method-calls/114443/2 "2007-09-12T19:38:43Z")

</div>

Check out profile.rb (in the standard lib), it will help you out with  
this.

Jason

---

<div class="post-metadata">

**Author:** ![s04015](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@s04015](https://www.ruby-forum.com/u/s04015)\
**Post date:** [September 12, 2007, 7:39pm UTC](https://www.ruby-forum.com/t/logging-all-method-calls/114443/3 "2007-09-12T19:39:43Z")

</div>

Gustav P. wrote:

> ```
> p class.name + method_name.to_s
> 
> ```
> 
> end  
> end

There’s a heavy speed cost to this, but it’s what your asking for…

## ri set\_trace\_func | cat -------------------------------------------------- Kernel#set\_trace\_func set\_trace\_func(proc) =\> proc set\_trace\_func(nil) =\> nil

```
  Establishes proc as the handler for tracing, or disables tracing
  if the parameter is nil. proc takes up to six parameters: an event
  name, a filename, a line number, an object id, a binding, and the
  name of a class. proc is invoked whenever an event occurs. Events
  are: c-call (call a C-language routine), c-return (return from a
  C-language routine), call (call a Ruby method), class (start a
  class or module definition), end (finish a class or module
  definition), line (execute code on a new line), raise (raise an
  exception), and return (return from a Ruby method). Tracing is
  disabled within the context of proc.

      class Test
      def test
        a = 1
        b = 2
      end
      end

      set_trace_func proc { |event, file, line, id, binding, 

```

classname|  
printf “%8s %s:%-2d %10s %8s\n”, event, file, line, id,  
classname  
}  
t = Test.new  
t.test

```
        line prog.rb:11 false
      c-call prog.rb:11 new Class
      c-call prog.rb:11 initialize Object
    c-return prog.rb:11 initialize Object
    c-return prog.rb:11 new Class
        line prog.rb:12 false
        call prog.rb:2 test Test
        line prog.rb:3 test Test
        line prog.rb:4 test Test
      return prog.rb:4 test Test
```

---

<div class="post-metadata">

**Author:** ![s04015](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/a88e4f/32.png) [@s04015](https://www.ruby-forum.com/u/s04015)\
**Post date:** [September 13, 2007, 10:41pm UTC](https://www.ruby-forum.com/t/logging-all-method-calls/114443/4 "2007-09-13T22:41:41Z")

</div>

ruby-prof
