Sha256: adcc830bd4856860b375f0dae539d1aea8ad0eb1ef86d21acfa833e8040f9dd4

Contents?: true

Size: 2 KB

Versions: 25

Compression:

Stored size: 2 KB

Contents

module Innate
  # Innate only provides logging via stdlib Logger to avoid bloat and
  # dependencies, you may specify multiple loggers in the Log instance of LogHub
  # to accomendate your needs, by default we log to $stderr to be compatible with
  # CGI.
  #
  # Please read the documentation of logger.rb (or even better, its source) to
  # get a feeling of how to use it correctly within Innate
  #
  # A few shortcuts:
  #
  # 1. Create logger for stderr/stdout
  #     logger = Logger.new($stdout)
  #     logger = Logger.new($stderr)
  #
  # 2. Create logger for a file
  #
  #     logger = Logger.new('test.log')
  #
  # 3. Create logger for file object
  #
  #     file = File.open('test.log', 'a+')
  #     logger = Logger.new(file)
  #
  # 4. Create logger with rotation on specified file size
  #
  #     # 10 files history, 5 MB each
  #     logger = Logger.new('test.log', 10, (5 << 20))
  #
  #     # 100 files history, 1 MB each
  #     logger = Logger.new('test.log', 100, (1 << 20))
  #
  # 5. Create a logger which ages logfiles daily/weekly/monthly
  #
  #     logger = Logger.new('test.log', 'daily')
  #     logger = Logger.new('test.log', 'weekly')
  #     logger = Logger.new('test.log', 'monthly')

  class LogHub
    include Logger::Severity
    include Optioned

    attr_accessor :loggers, :program, :active

    # +loggers+ should be a list of Logger instances
    def initialize(*loggers)
      @loggers = loggers.flatten
      @program = nil
      @active = true
      self.level = DEBUG
    end

    # set level for all loggers
    def level=(lvl)
      @loggers.each{|l| l.level = lvl }
      @level = lvl
    end

    def start; @active = true;  end
    def stop;  @active = false; end

    def method_missing(meth, *args, &block)
      eval %~
        def #{meth}(*args, &block)
          return unless @active
          args.each{|arg| @loggers.each{|logger| logger.#{meth}(arg, &block) }}
        end
      ~

      send(meth, *args, &block)
    end

    def write(*args)
      self.<<(*args)
    end
  end
end

Version data entries

25 entries across 25 versions & 3 rubygems

Version Path
innate-2023.01.06 lib/innate/log/hub.rb
innate-2015.10.28 lib/innate/log/hub.rb
manveru-innate-2009.06.12 lib/innate/log/hub.rb
manveru-innate-2009.06 lib/innate/log/hub.rb
manveru-innate-2009.07 lib/innate/log/hub.rb
rjspotter-innate-2009.06.29 lib/innate/log/hub.rb
rjspotter-innate-2009.06.30 lib/innate/log/hub.rb
rjspotter-innate-2009.06.31 lib/innate/log/hub.rb
innate-2013.02.21 lib/innate/log/hub.rb
innate-2013.02 lib/innate/log/hub.rb
innate-2012.12 lib/innate/log/hub.rb
innate-2012.03 lib/innate/log/hub.rb
innate-2011.12 lib/innate/log/hub.rb
innate-2011.10 lib/innate/log/hub.rb
innate-2011.04 lib/innate/log/hub.rb
innate-2011.01 lib/innate/log/hub.rb
innate-2010.07 lib/innate/log/hub.rb
innate-2010.06.18 lib/innate/log/hub.rb
innate-2010.04 lib/innate/log/hub.rb
innate-2010.03 lib/innate/log/hub.rb