Class: Stretchy::Utils::Logger

Inherits:
Object
  • Object
show all
Includes:
Contract
Defined in:
lib/stretchy/utils/logger.rb

Constant Summary collapse

LEVELS =
[:silence, :debug, :info, :warn, :error, :fatal]

Constants included from Contract

Contract::ASSERTIONS, Contract::DECAY_FUNCTIONS, Contract::DISTANCE_FORMAT

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Contract

included, #require_one, #validate!

Constructor Details

#initialize(base = nil, level = nil, color = nil) ⇒ Logger

Returns a new instance of Logger.



22
23
24
25
26
27
28
29
# File 'lib/stretchy/utils/logger.rb', line 22

def initialize(base = nil, level = nil, color = nil)
  @base  = base       || ::Logger.new(STDOUT)
  @level = level      || :silence
  @color = color      || :blue

  @color = @color.to_s if @color.is_a?(Symbol)
  validate!
end

Instance Attribute Details

#baseObject

Returns the value of attribute base.



12
13
14
# File 'lib/stretchy/utils/logger.rb', line 12

def base
  @base
end

#colorObject

Returns the value of attribute color.



12
13
14
# File 'lib/stretchy/utils/logger.rb', line 12

def color
  @color
end

#levelObject

Returns the value of attribute level.



12
13
14
# File 'lib/stretchy/utils/logger.rb', line 12

def level
  @level
end

Class Method Details

.log(msg_or_obj) ⇒ Object



18
19
20
# File 'lib/stretchy/utils/logger.rb', line 18

def self.log(msg_or_obj)
  self.new.log(msg_or_obj)
end

Instance Method Details

#log(msg_or_obj, _level = nil, _color = nil) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/stretchy/utils/logger.rb', line 31

def log(msg_or_obj, _level = nil, _color = nil)
  _level ||= level
  _color ||= color
  
  return if _level == :silence
  output = nil

  case msg_or_obj
  when String
    output = Colorize.send(_color, msg_or_obj)
  when Hash, Array
    output = Colorize.send(_color, JSON.pretty_generate(msg_or_obj))
  when Stretchy::Boosts::Base, Stretchy::Filters::Base, Stretchy::Queries::Base
    output = Colorize.send(_color, JSON.pretty_generate(msg_or_obj.to_search))
  else
    output = Colorize.send(_color, msg_or_obj.inspect)
  end

  base.send(_level, output)
end