How to get the name of the current method to output it to the log file?

I am using Ruby on Rails 3.2.2 and I use logger.warn in my code to display warning messages for development purposes. I would like to get the name of the method this logger.warn to print this 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 get and display ClassName # method_name

Is it possible? If so, how to do it?

+6
source share
1 answer
 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.

+23
source

Source: https://habr.com/ru/post/915000/


All Articles