Sha256: f7b8ec2cbf594ceb1b7dbf5b46cc1a9aff654b9af9fa0cf51a441ff14fabd289

Contents?: true

Size: 974 Bytes

Versions: 12

Compression:

Stored size: 974 Bytes

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)
  end

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

  def error obj
    write "ERROR: " + obj_to_string(obj)
  end
  
  def debug obj
    write obj_to_string(obj).gsub(/BSON::ObjectId\(([a-z0-9'"]+)\)/, "\\1")
  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.message}
  #{backtrace.join("\n  ")}
} 
      else
        obj.to_s
      end
    end
end

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
rad_core-0.2.6 lib/rad/environment/_logger.rb
rad_core-0.2.5 lib/rad/environment/_logger.rb
rad_core-0.2.4 lib/rad/environment/_logger.rb
rad_core-0.2.3 lib/rad/environment/_logger.rb
rad_core-0.2.2 lib/rad/environment/_logger.rb
rad_core-0.2.1 lib/rad/environment/_logger.rb
rad_core-0.2.0 lib/rad/environment/_logger.rb
rad_core-0.0.30 lib/rad/environment/_logger.rb
rad_core-0.0.29 lib/rad/environment/_logger.rb
rad_core-0.0.28 lib/rad/environment/_logger.rb
rad_core-0.0.27 lib/rad/environment/_logger.rb
rad_core-0.0.26 lib/rad/environment/_logger.rb