Class: Logger::Logging::ColoredLogger

Inherits:
ExtendedLogger show all
Defined in:
lib/logger/logging/colored_logger.rb

Constant Summary collapse

ANSI_COLORS =
%i(black red green yellow blue magenta cyan white)
DEFAULT_PALETTE =
{
  "ANY"   => -> msg { bg(:blue, :normal, fg(:white, :bright, msg)) },
  "FATAL" => -> msg { bg(:red, :bright, fg(:white, :bright, msg)) },
  "ERROR" => -> msg { fg(:red, :bright, msg) },
  "WARN"  => -> msg { fg(:yellow, :normal, msg) },
  "INFO"  => -> msg { fg(:green, :normal, msg) },
  "DEBUG" => -> msg { fg(:blue, :normal, msg) },
  "TRACE" => -> msg { fg(:magenta, :bright, msg) },
  "DATA"  => -> msg { fg(:cyan, :normal, msg) },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ExtendedLogger

#data, #format_severity, #io, #trace

Instance Attribute Details

#paletteObject



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

def palette
  @palette or DEFAULT_PALETTE
end

Instance Method Details

#bg(*args) ⇒ Object



18
19
20
# File 'lib/logger/logging/colored_logger.rb', line 18

def bg *args
  col :bg, *args
end

#col(fgbg, color_code, intensity_code, str) ⇒ Object



22
23
24
25
26
27
# File 'lib/logger/logging/colored_logger.rb', line 22

def col fgbg, color_code, intensity_code, str
  color_num = ANSI_COLORS.index color_code
  intensity_num = { :normal => 0, :bright => 1 }.fetch intensity_code
  fgbg_num = { :fg => 3, :bg => 4 }.fetch fgbg
  "\e[#{intensity_num};#{fgbg_num}#{color_num}m#{str}\e[0m"
end

#fg(*args) ⇒ Object



29
30
31
# File 'lib/logger/logging/colored_logger.rb', line 29

def fg *args
  col :fg, *args
end

#format_message(severity, *args, progname, message) ⇒ Object



33
34
35
36
37
38
# File 'lib/logger/logging/colored_logger.rb', line 33

def format_message severity, *args, progname, message
  formatter = palette.fetch severity
  colored_message = instance_exec message, &formatter
  colored_progname = instance_exec progname, &formatter
  super severity, *args, colored_progname, colored_message
end