Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby 如何定义在其他模块之间共享的通用模块日志记录_Ruby_Logging_Module - Fatal编程技术网

Ruby 如何定义在其他模块之间共享的通用模块日志记录

Ruby 如何定义在其他模块之间共享的通用模块日志记录,ruby,logging,module,Ruby,Logging,Module,我正在开发一个带有一个大的主函数的脚本,我已经将它分为几个模块。 我需要的是从他们所有人那里访问日志功能,这意味着日志文件只需打开一次,访问权限可以共享 这就是我所拥有的: require 'module_1' require 'module_2' require 'module_3' module Main Module_1.Function_startup() Module_2.Function_configuration() Module_3.Function_self_test(

我正在开发一个带有一个大的主函数的脚本,我已经将它分为几个模块。 我需要的是从他们所有人那里访问日志功能,这意味着日志文件只需打开一次,访问权限可以共享

这就是我所拥有的:

require 'module_1'
require 'module_2'
require 'module_3'

module Main
 Module_1.Function_startup()
 Module_2.Function_configuration()
 Module_3.Function_self_test()
end
这是我需要的记录器的脏模块,在所有其他模块中都可用。 理想情况下,我希望将其称为“logger.error”,其中“logger”返回记录器的实例,“error”是对rlogger的函数调用,即rlogger.error

require 'logger'

module Logging
  @rlogger = nil

    def init_logger
      if @rlogger.nil?
        puts "initializing logger"
        file_path = File.join(File.dirname(__FILE__), 'checker.log')
        open_mode = File::TRUNC # or File::APPEND
        file = File.open(file_path, File::WRONLY | open_mode)

        @rlogger = Logger.new(file)
        @rlogger.datetime_format = "%Y-%m-%d %H:%M:%S"

        @rlogger.formatter = proc do |severity, datetime, progname, msg|
          con_msg = ""
          if msg.match("ERROR:")
            con_msg = msg.color(:red)
          elsif msg.match("OK!")
            con_msg = msg.color(:green)
          else
            con_msg = msg
          end
          puts ">>>#{con_msg}"
          # notice that the colors introduce extra non-printable characters
          # which are not nice in the log file.
          "#{datetime}: #{msg}\n"
        end

        # Here is the first log entry
        @rlogger.info('Initialize') {"#{Time.new.strftime("%H-%M-%S")}: Checker v#{@version}"}
      end
    end

    # returns the logger
    def logger
      if @rlogger.nil?
        puts "requesting nil rlogger"
      end
      @rlogger
    end
  end
end

在需要之后,您可以添加这段代码

$FILE_LOG = Logging.create_log(File.expand_path('LoggingFile.log'), Logger::DEBUG)
上面一行的解释:它正在调用日志模块中的一个函数,以创建文件,日志级别为调试

下面是模块的一段代码

module Logging
  def self.create_log(output_location level)
    log = Logger.new(output_location, 'weekly').tap do |l|
      next unless l

      l.level = level

      l.progname = File.basename($0)

      l.datetime_format = DateTime.iso8601

      l.formatter = proc { |severity, datetime, progname, msg| "#{severity}: #{datetime} - (#{progname}) : #{msg}\n" }
    end

    log.level = level if level < log.level
    log
  end


  def self.log(msg, level)
    # Here I am just logging only FATAL and DEBUG, similarly you can add in different level of logs
    if level == :FATAL
      $FILE_LOG.fatal(msg)
    elsif level == :DEBUG
      $FILE_LOG.debug(msg)
    end
  end
end
Logging.log("Message",:FATAL)