Class: Logger::ColorFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/innate/log/color_formatter.rb

Overview

Extended Formatter that supports ANSI colors.

Constant Summary collapse

LEVEL_COLOR =
{
  'DEBUG'   => :blue,
  'INFO'    => :white,
  'WARN'    => :yellow,
  'ERROR'   => :red,
  'FATAL'   => :red,
  'UNKNOWN' => :green,
}
COLOR_CODE =
{
  :reset => 0, :bold => 1, :dark => 2, :underline => 4, :blink => 5,
  :negative => 7, :black => 30, :red => 31, :green => 32, :yellow => 33,
  :blue => 34, :magenta => 35, :cyan => 36, :white => 37,
}
FORMAT_TIME =
"%Y-%m-%d %H:%M:%S"
FORMAT_LINE =
"%s [%s $%d] %5s | %s: %s\n"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.color?(logdev) ⇒ Boolean

Returns:

  • (Boolean)


39
40
41
# File 'lib/innate/log/color_formatter.rb', line 39

def self.color?(logdev)
  logdev.respond_to?(:tty?) and logdev.tty?
end

Instance Method Details

#call(severity, time, program, message) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/innate/log/color_formatter.rb', line 22

def call(severity, time, program, message)
  hint = severity[0,1]
  time = format_time(time)
  pid = $$
  string = colorize(msg2str(message), severity)

  FORMAT_LINE % [hint, time, pid, severity, program, string]
end

#colorize(string, severity) ⇒ Object



35
36
37
# File 'lib/innate/log/color_formatter.rb', line 35

def colorize(string, severity)
  "\e[#{COLOR_CODE[LEVEL_COLOR[severity]]}m#{string}\e[0m"
end

#format_time(time) ⇒ Object



31
32
33
# File 'lib/innate/log/color_formatter.rb', line 31

def format_time(time)
  time.strftime(FORMAT_TIME)
end