Get current stack trace in Ruby without raising an exception

后端 未结 3 2073
既然无缘
既然无缘 2020-12-07 13:23

I want to log the current backtrace (stacktrace) in a Rails 3 app without an exception occurring. Any idea how?

Why do I want this? I\'m trying to trace the

3条回答
  •  情歌与酒
    2020-12-07 14:25

    You can use Kernel#caller:

    # /tmp/caller.rb
    
    def foo 
      puts caller # Kernel#caller returns an array of strings
    end
    
    def bar 
      foo 
    end
    
    def baz 
      bar 
    end
    
    baz
    

    Output:

    caller.rb:8:in `bar'
    caller.rb:12:in `baz'
    caller.rb:15:in `
    '

提交回复
热议问题