Class: Logger

Inherits:
Object
  • Object
show all
Defined in:
lib/terrimporter/app_logger.rb

Constant Summary collapse

LOG_LEVELS =
{:debug => 0, :info => 1, :warn => 2, :error=> 3, :fatal => 4}
LOG_COLORS =
{:debug =>'33', :info =>'32', :warn =>'33', :error=>'31', :fatal =>'31'}
LOG_FORMAT =

%s => [datetime], %s => color, %-5s => severity, %s => message

"\033[0;37m %s \033[0m[\033[%sm%-5s\033[0m]: %s \n"
TIME_FORMAT =
"%H:%M:%S"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeLogger

Returns a new instance of Logger.



19
20
21
# File 'lib/terrimporter/app_logger.rb', line 19

def initialize
  self.level = :debug
end

Instance Attribute Details

#levelObject

Returns the value of attribute level.



2
3
4
# File 'lib/terrimporter/app_logger.rb', line 2

def level
  @level
end

Instance Method Details

#debug(message) ⇒ Object



31
32
33
# File 'lib/terrimporter/app_logger.rb', line 31

def debug(message)
  log(:debug, message)
end

#error(message) ⇒ Object



23
24
25
# File 'lib/terrimporter/app_logger.rb', line 23

def error(message)
  log(:error, message)
end

#format_datetime(time) ⇒ Object



46
47
48
# File 'lib/terrimporter/app_logger.rb', line 46

def format_datetime(time)
  time.strftime(TIME_FORMAT)
end

#info(message) ⇒ Object



27
28
29
# File 'lib/terrimporter/app_logger.rb', line 27

def info(message)
  log(:info, message)
end

#log(severity, message) ⇒ Object



35
36
37
38
39
40
41
42
43
44
# File 'lib/terrimporter/app_logger.rb', line 35

def log(severity, message)
  return if LOG_LEVELS[severity] < LOG_LEVELS[self.level]

  color = LOG_COLORS[severity]
  if LOG_LEVELS[severity] >= LOG_LEVELS[:error]
    $stderr.puts(LOG_FORMAT % [format_datetime(Time.now), color, severity.to_s.upcase, message])
  else
    $stdout.puts(LOG_FORMAT % [format_datetime(Time.now), color, severity.to_s.upcase, message])
  end
end