1

这是我用来记录我的状态的 ruby​​ 脚本。

require 'logger'

log = Logger.new( 'log.txt', 'daily' )

log.debug "Once the log becomes at least one"
log.debug "day old, it will be renamed and a"
log.debug "new log.txt file will be created."

现在我想制作一个新的 ruby​​ 文件,我想在其中包含 logger 它可以是这样的

module Logging

  def logger
    Logging.logger
  end

  def self.logger
    @logger ||= Logger.new(STDOUT)
  end

end

但我无法理解这一点,所以任何人都可以解释一下。

我的要求就像有很多 ruby​​ 脚本文件我想在模块中放置一个记录器并将其包含在每个文件中,而不是在日志文件中写入日志,它可能会警告它可能是信息或其他任何内容。

4

2 回答 2

0

我可以从你的问题中收集到的是代码的工作原理......你只是对它的作用感到困惑......如果这是正确的,那么解释一下,当你inculde Logging在你的任何类中从类方法中的任何地方调用logger.log("This is a log message")它时将该消息记录到启动脚本的控制台。

这是否回答你的问题?

于 2012-09-13T07:21:19.443 回答
0

更好的选择是:

module Logging

def logger
@logger ||= Logger.new(STDOUT)
end

end

并像这样使用它

class MyLoggingClient
include Logging

#Now you have access to the method `logger` and the instance variable `@logger`.

end
于 2012-09-13T07:23:46.327 回答