Sha256: 6cce96e8181fef35c9e3e6dd5c9bc2bb96556015ff54162243387db45574f7be

Contents?: true

Size: 1.08 KB

Versions: 2

Compression:

Stored size: 1.08 KB

Contents

module CubaApi
  
  module Loggers
    module ClassMethods

      def logger_factory
        self[ :loggers ] ||=  CubaApi::LoggerFactory.new
      end

    end

    def logger_factory
      self.class.logger_factory
    end
  end

  class LoggerFactory

    def self.level=( level )
      @level = level
    end

    def self.logger( cat )
      loggers[ cat ] ||= Logger.new( cat, @level )
    end

    private

    def self.loggers
      @loggers ||= {}
    end
  end

  class Logger

    private

    def puts( level, &block )
      if level >= @level
        Kernel.puts( "#{@cat} #{block.call}" )
      end
    end

    def warn( level, &block )
      if level >= @level
        Kernel.puts( "#{@cat} #{block.call}" )
      end
    end

    public

    def initialize( cat = 'ROOT', level = 1 )
      @level = ( level || 1 ).to_i
      @cat = cat
    end

    def debug( &block )
      puts ( 0, &block )
    end
 
    def info( &block )
      puts ( 1, &block )
    end
 
    def warn( &block )
      warn ( 2, &block )
    end

    def error( &block )
      warn ( 3, &block )
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
cuba-api-0.6.3 lib/cuba_api/loggers.rb~
cuba-api-0.6.2 lib/cuba_api/loggers.rb~