我如何制作这个在日志文件中输出一条黄线的方法,可以从我的 Rails 应用程序的任何地方(模型、 Controller 、 View )访问?
def my_log(text, file = "", line = "")
text.to_s.chomp.gsub!(/%/, "%%")
Rails.logger.debug(sprintf("\033[32m#{file}#{line}\033[0m\033[1m\033[33m#{text}\033[0m"))
end
最佳答案
您可以在 Kernel
中定义它(不建议):
module Kernel
def my_log(..)
..
end
end
...如果你真的希望它可以在任何地方使用。
或者,将类似的内容放入
lib/util.rb
:module Util
def self.my_log(..)
..
end
end
...并确保
require 'util'
在您的 config/application.rb
然后你可以在任何地方调用它:Util.my_log(..)
关于ruby-on-rails - Rails 中随处可见的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6227772/