Sha256: 861914c254cbdb3bdc2e30b1ddb68ec14d8ff5f4c2ae6e25946ad243a6dd352a

Contents?: true

Size: 1.02 KB

Versions: 9

Compression:

Stored size: 1.02 KB

Contents

class Rad::Logger        
  def initialize stream = nil
    @stream = stream
  end
  
  def silence &b
    begin
      @silence = true
      b.call
    ensure
      @silence = false
    end    
  end
  
  def info obj
    write obj_to_string(obj) + "\n"
  end

  def warn obj
    write "WARN: " + obj_to_string(obj) + "\n"
  end

  def error obj
    write "ERROR: " + obj_to_string(obj) + "\n"
  end
  
  def debug obj
    write obj_to_string(obj).gsub(/BSON::ObjectId\(([a-z0-9'"]+)\)/, "\\1") + "\n"
  end

  protected
    def write str
      if !@silence
        str = indent(str)
        stream ? stream.write(str) : puts(str)
      end
    end
  
    IDENTATION = "  "
    attr_reader :stream
          
    def indent string
      IDENTATION + string.gsub("\n", "\n" + IDENTATION)
    end
    
    def obj_to_string obj
      if obj.is_a? Exception
        backtrace = (obj.backtrace && obj.backtrace.sfilter(Exception.filters)) || []
        %{\
#{obj.message}
  #{backtrace.join("\n  ")}
} 
      else
        obj.to_s
      end
    end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
rad_core-0.0.25 lib/rad/environment/_logger.rb
rad_core-0.0.24 lib/rad/environment/_logger.rb
rad_core-0.0.23 lib/rad/environment/_logger.rb
rad_core-0.0.22 lib/rad/environment/_logger.rb
rad_core-0.0.21 lib/rad/environment/_logger.rb
rad_core-0.0.20 lib/rad/environment/_logger.rb
rad_core-0.0.19 lib/rad/environment/_logger.rb
rad_core-0.0.18 lib/rad/environment/_logger.rb
rad_core-0.0.17 lib/rad/environment/_logger.rb