如何检索当前方法名称,以便将其输出到记录器文件? [英] How to retrieve the current method name so to output it to the logger file?

查看:59
本文介绍了如何检索当前方法名称,以便将其输出到记录器文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Ruby on Rails 3.2.2,并且为了出于开发目的显示警告消息,我在代码中使用了logger.warn.我想检索运行logger.warn方法名称,以便将该方法名称输出到日志文件.

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:

我想检索并显示ClassName#method_name

I would like to retrieve and display the ClassName#method_name

有可能吗?如果是这样,我该怎么做?

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

推荐答案

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

这应该可以完成工作.

这篇关于如何检索当前方法名称,以便将其输出到记录器文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆