Class: Outlog::ConsoleLogger

Inherits:
BaseLogger show all
Defined in:
lib/outlog/console_logger.rb

Constant Summary collapse

CODEMAP =

Mapping of color/style names to ANSI control values

{
  normal: 0,
  bold: 1,
  black: 30,
  red: 31,
  green: 32,
  yellow: 33,
  blue: 34,
  magenta: 35,
  cyan: 36,
  white: 37
}
LEVELMAP =

Map of log levels to colors

{
  "FATAL" => :red,
  "ERROR" => :red,
  "WARN" => :yellow,
  "INFO" => :green, # default color
  "DEBUG" => :normal
}

Instance Method Summary collapse

Methods inherited from BaseLogger

#debug, #error, #fatal, #info, #level, #level=, #level_name, #level_symbol, #warn

Instance Method Details

#output(hash) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/outlog/console_logger.rb', line 26

def output hash
  arr = [hash[:message]]
  hash.each_pair { |k,v|
    next if %i[ ts level environment hostname pid message ].include? k
    arr << " #{k}="
    arr << JSON.dump(v)
  }
  color = LEVELMAP[hash[:level]] || :green

  arr.unshift "\e[#{CODEMAP[color]}m"
  arr << "\e[#{CODEMAP[:normal]}m"

  puts arr.join
  $stdout.flush
end