Add logger to module/class

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

···

--
Posted via http://www.ruby-forum.com/.

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.

docos => File: README — Documentation for logging (2.3.1)
source => GitHub - TwP/logging: A flexible logging library for use in Ruby programs based on the design of Java's log4j library.

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.

Blessings,
TwP

···

On Oct 25, 2011, at 9:34 AM, Kassym Dorsel wrote: