Sha256: fd3cf2625c469bbc6200bcd8cce8ca0ceb6632edaef773184f5c6b5c68b8841f

Contents?: true

Size: 1.32 KB

Versions: 36

Compression:

Stored size: 1.32 KB

Contents

module Redcar
  class Logger
    LEVELS = [:debug, :info, :warn, :error]
    
    def initialize(*targets)
      @targets = targets
    end
    
    def level
      @level || :info
    end
    
    def level=(val)
      @level = val ? val.to_sym : nil
    end
    
    def close
      @targets.each {|target| target.close if target.is_a?(File)}
    end
    
    def log(msg_level, msg)
      string = "#{msg_level.to_s.rjust(5, " ")} [#{Time.now.strftime("%x %X")}] #{msg}"
      @targets.each do |target|
        target.puts(string)
      end
    end
    
    def should_log?(msg_level)
      LEVELS.index(msg_level) >= LEVELS.index(level)
    end
    
    def error(msg=nil)
      return unless should_log?(:error)
      log(:error, (block_given? ? yield : msg))
    end
    
    def warn(msg=nil)
      return unless should_log?(:warn)
      log(:warn, (block_given? ? yield : msg))
    end
    
    def info(msg=nil)
      return unless should_log?(:info)
      log(:info, (block_given? ? yield : msg))
    end
    
    def debug(msg=nil)
      return unless should_log?(:debug)
      log(:debug, (block_given? ? yield : msg))
    end
    
    def benchmark(msg, msg_level=:debug)
      s = Time.now
      result = yield
      if should_log?(msg_level)
        log(msg_level, msg + " (#{Time.now - s}s)")
      end
      result
    end
  end
end


Version data entries

36 entries across 36 versions & 2 rubygems

Version Path
redcar-0.13 lib/redcar/logger.rb
redcar-dev-0.13.5dev lib/redcar/logger.rb
redcar-dev-0.13.4dev lib/redcar/logger.rb
redcar-dev-0.13.3dev lib/redcar/logger.rb
redcar-dev-0.13.2dev lib/redcar/logger.rb
redcar-dev-0.13.1dev lib/redcar/logger.rb
redcar-0.12.1 lib/redcar/logger.rb
redcar-dev-0.13.0dev lib/redcar/logger.rb
redcar-0.12 lib/redcar/logger.rb
redcar-dev-0.12.27dev lib/redcar/logger.rb
redcar-dev-0.12.26dev lib/redcar/logger.rb
redcar-dev-0.12.25dev lib/redcar/logger.rb
redcar-dev-0.12.24dev lib/redcar/logger.rb
redcar-dev-0.12.23dev lib/redcar/logger.rb
redcar-dev-0.12.22dev lib/redcar/logger.rb
redcar-dev-0.12.21dev lib/redcar/logger.rb
redcar-dev-0.12.20dev lib/redcar/logger.rb
redcar-dev-0.12.19dev lib/redcar/logger.rb
redcar-dev-0.12.18dev lib/redcar/logger.rb
redcar-dev-0.12.17dev lib/redcar/logger.rb