I made a simple class to wrap native logger:
#logging.rb
require 'logger'
class Log
def initialize(lname = 'log')
@logger = Logger.new('logs/'+lname+'.txt')
@logger.level = Logger::INFO
end
def Info(msg, print = true)
@logger.info(msg)
if print
puts msg
end
end
def Warning(msg, print = true)
@logger.warn(msg)
if print
puts msg
end
end
end
and then I use it in a script like so
require_relative 'logging'
require_relative 'anotherclass'
@logger = Log.new("Mylog_"+Time.now.strftime('%Y-%m-%d_%H-%M-%S'))
@logger.Info('Starting')
@anotherclass = AnotherClass.new()
This all works fine, but I would like to be able to use the same logger object in other classes, with the objective being the same log file will be appended to, instead of creating another file.
So if I make another class
#anotherclass.rb
require_relative 'logging'
class AnotherClass
def initialize()
Log.Info("Log to the same log file")
end
end
How can I get it to use the previously created Log class?