7

私はRubyonRails 3.2.2を使用しており、開発目的で警告メッセージを表示するためにlogger.warn、コードで使用しています。それが実行されているメソッド名を取得して、logger.warnそのメソッド名をログファイルに出力したいと思います。

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

ログファイルで私が見たいのは:

ClassName#method_nameを取得して表示したい

出来ますか?もしそうなら、どうすればそれを作ることができますか?

4

1 に答える 1

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

これでうまくいくはずです。

于 2012-05-06T03:00:00.510 に答える