Class: Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/esearchy/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(level, output) ⇒ Logger

Returns a new instance of Logger.



3
4
5
6
7
8
9
10
11
# File 'lib/esearchy/logger.rb', line 3

def initialize(level, output)
  @level = level
  if output.class == IO
    @device = output
  else
    @device = File.new(output, "a")
  end
  @device.sync = true
end

Instance Attribute Details

#deviceObject

Returns the value of attribute device.



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

def device
  @device
end

#levelObject

Returns the value of attribute level.



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

def level
  @level
end

Instance Method Details

#clean(msg) ⇒ Object



24
25
26
# File 'lib/esearchy/logger.rb', line 24

def clean(msg)
  msg.match(/\n|\r/) != nil ? msg.strip!  : msg
end

#closeObject



28
29
30
# File 'lib/esearchy/logger.rb', line 28

def close
 @device.close if @open
end

#puts(msg) ⇒ Object



14
15
16
17
18
19
20
21
22
# File 'lib/esearchy/logger.rb', line 14

def puts(msg)
  if @level > 1
    begin
      @device.puts clean(msg) + "\n"
    rescue
      Raise "Something went Wrong writing to [file|IO]"
    end
  end
end