Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to retrieve the current method name so to output it to the logger file?

Tags:

methods

ruby

I am using Ruby on Rails 3.2.2 and, in order to display warning messages for development purposes, I am using logger.warn in my code. I would like to retrieve the method name in which that logger.warn runs so to output that method name to the log file.

class ClassName < ActiveRecord::Base
  def method_name
    # Note: This code doesn't work. It is just a sample.
    logger.warn "I would like to retrieve and display the #{self.class.to_s}##{method_name}"
  end
end

In the log file I would like to see:

I would like to retrieve and display the ClassName#method_name

Is it possible? If so, how can I make that?

like image 363
user12882 Avatar asked May 06 '12 02:05

user12882


1 Answers

class ClassName < ActiveRecord::Base
  def method_name
    logger.warn("I would like to retrieve and display the #{self.class}##{__method__}")
  end
end

this should do the job.

like image 142
Bhushan Lodha Avatar answered Nov 06 '22 02:11

Bhushan Lodha