0
여러 개의 모듈로 분할 된 큰 주 기능이있는 스크립트를 개발 중입니다. 필요한 모든 기능은 로그 기능에 액세스하는 것입니다. 즉, 로그 파일을 한 번만 열고 액세스 권한을 공유해야합니다.공용 모듈을 정의하는 방법 모듈간에 공유 공유
이 내가 가진 것입니다 : 여기
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