Class: SwitchTower::Logger

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

Overview

:nodoc:

Constant Summary collapse

IMPORTANT =
0
INFO =
1
DEBUG =
2
TRACE =
3

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Logger

Returns a new instance of Logger.



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/switchtower/logger.rb', line 10

def initialize(options={})
  output = options[:output] || STDERR
  case
    when output.respond_to?(:puts)
      @device = output
    else
      @device = File.open(output.to_str, "a")
      @needs_close = true
  end

  @options = options
  @level = 0
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



3
4
5
# File 'lib/switchtower/logger.rb', line 3

def level
  @level
end

Instance Method Details

#closeObject



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

def close
  @device.close if @needs_close
end

#debug(message, line_prefix = nil) ⇒ Object



48
49
50
# File 'lib/switchtower/logger.rb', line 48

def debug(message, line_prefix=nil)
  log(DEBUG, message, line_prefix)
end

#important(message, line_prefix = nil) ⇒ Object



40
41
42
# File 'lib/switchtower/logger.rb', line 40

def important(message, line_prefix=nil)
  log(IMPORTANT, message, line_prefix)
end

#info(message, line_prefix = nil) ⇒ Object



44
45
46
# File 'lib/switchtower/logger.rb', line 44

def info(message, line_prefix=nil)
  log(INFO, message, line_prefix)
end

#log(level, message, line_prefix = nil) ⇒ Object



28
29
30
31
32
33
34
35
36
37
38
# File 'lib/switchtower/logger.rb', line 28

def log(level, message, line_prefix=nil)
  if level <= self.level
    if line_prefix
      message.split(/\r?\n/).each do |line|
        @device.print "[#{line_prefix}] #{line.strip}\n"
      end
    else
      @device.puts message.strip
    end
  end
end

#trace(message, line_prefix = nil) ⇒ Object



52
53
54
# File 'lib/switchtower/logger.rb', line 52

def trace(message, line_prefix=nil)
  log(TRACE, message, line_prefix)
end