Class: God::SimpleLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/god/simple_logger.rb

Direct Known Subclasses

Logger

Constant Summary collapse

DEBUG =
2
INFO =
4
WARN =
8
ERROR =
16
FATAL =
32
SEV_LABEL =
{ DEBUG => 'DEBUG',
INFO => 'INFO',
WARN => 'WARN',
ERROR => 'ERROR',
FATAL => 'FATAL' }.freeze
CONSTANT_TO_SYMBOL =
{ DEBUG => :debug,
INFO => :info,
WARN => :warn,
ERROR => :error,
FATAL => :fatal }.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io) ⇒ SimpleLogger

Returns a new instance of SimpleLogger.



25
26
27
28
29
# File 'lib/god/simple_logger.rb', line 25

def initialize(io)
  @io = io
  @level = INFO
  @datetime_format = '%Y-%m-%d %H:%M:%S'
end

Instance Attribute Details

#datetime_formatObject

Returns the value of attribute datetime_format.



23
24
25
# File 'lib/god/simple_logger.rb', line 23

def datetime_format
  @datetime_format
end

#levelObject

Returns the value of attribute level.



23
24
25
# File 'lib/god/simple_logger.rb', line 23

def level
  @level
end

Instance Method Details

#debug(msg) ⇒ Object



55
56
57
# File 'lib/god/simple_logger.rb', line 55

def debug(msg)
  output(DEBUG, msg)
end

#error(msg) ⇒ Object



43
44
45
# File 'lib/god/simple_logger.rb', line 43

def error(msg)
  output(ERROR, msg)
end

#fatal(msg) ⇒ Object



39
40
41
# File 'lib/god/simple_logger.rb', line 39

def fatal(msg)
  output(FATAL, msg)
end

#info(msg) ⇒ Object



51
52
53
# File 'lib/god/simple_logger.rb', line 51

def info(msg)
  output(INFO, msg)
end

#output(level, msg) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/god/simple_logger.rb', line 31

def output(level, msg)
  return if level < self.level

  time = Time.now.strftime(datetime_format)
  label = SEV_LABEL[level]
  @io.print("#{label[0..0]} [#{time}] #{label.rjust(5)}: #{msg}\n")
end

#warn(msg) ⇒ Object



47
48
49
# File 'lib/god/simple_logger.rb', line 47

def warn(msg)
  output(WARN, msg)
end