I want a dual logger so I have sub-classed Logger with this :
class Logging < Logger
def initialize(file, stdout=false) @log_file = Logger(file) @log_out = Logger(stdout) if stdout
end #Override the logger's methods to write to both stdout and file if
needed
end
How would I implement a global logger for the module and the main. The
logger would be initiated within the main and I'd want to pass it to the
module with the same parameters, given a module like this :
module A
def self.a
end
# Anywhere inside the module I want to be able to do logger.info(str)
and it will log to same logger as initialized in main.
class c
def ca
end
end
end
I want a dual logger so I have sub-classed Logger with this :
class Logging < Logger
def initialize(file, stdout=false) @log_file = Logger(file) @log_out = Logger(stdout) if stdout
end #Override the logger's methods to write to both stdout and file if
needed
end
Take a look at the "Logging" gem as it already support logging to multiple destinations. The interface for logger objects is the same as the core Ruby logger class.
How would I implement a global logger for the module and the main. The
logger would be initiated within the main and I'd want to pass it to the
module with the same parameters, given a module like this :
One trick you can use with the Logging framework is this
require 'logging'
include Logging.globally
This will add a "logger" method to Object which will be inherited by every object in your application. This will allow you to do call "logger.info" within your module or any other class.