Class: FTest::ColoredLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/ftest/colored_logger.rb

Constant Summary collapse

ANSI_COLORS =
%i(black red green yellow blue magenta cyan white)
DEFAULT_PALETTE =
{
  :unknown => -> msg { bg(:blue, :bright, fg(:white, :bright, msg)) },
  :fatal   => -> msg { bg(:red, :bright, fg(:white, :bright, msg)) },
  :error   => -> msg { fg(:red, :bright, msg) },
  :warn    => -> msg { fg(:yellow, :bright, msg) },
  :info    => -> msg { fg(:default, :normal, msg) },
  :debug   => -> msg { fg(:cyan, :bright, msg) },
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(io, palette = DEFAULT_PALETTE) ⇒ ColoredLogger

Returns a new instance of ColoredLogger.



16
17
18
19
# File 'lib/ftest/colored_logger.rb', line 16

def initialize io, palette = DEFAULT_PALETTE
  @logger  = Logger.new io
  @palette = palette
end

Instance Attribute Details

#paletteObject (readonly)

Returns the value of attribute palette.



14
15
16
# File 'lib/ftest/colored_logger.rb', line 14

def palette
  @palette
end

Instance Method Details

#bg(*args) ⇒ Object



51
52
53
# File 'lib/ftest/colored_logger.rb', line 51

def bg *args
  col :bg, *args
end

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



55
56
57
58
59
60
# File 'lib/ftest/colored_logger.rb', line 55

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[#{fgbg_num}#{color_num};#{intensity_num}m#{str}\e[0m"
end

#fg(*args) ⇒ Object



47
48
49
# File 'lib/ftest/colored_logger.rb', line 47

def fg *args
  col :fg, *args
end

#format(log_level, msg) ⇒ Object



42
43
44
45
# File 'lib/ftest/colored_logger.rb', line 42

def format log_level, msg
  formatter = palette.fetch log_level
  instance_exec msg, &formatter
end