Module: RSMP::Logger::Colorization

Included in:
RSMP::Logger
Defined in:
lib/rsmp/log/colorization.rb

Overview

Handles colorization of log output

Instance Method Summary collapse

Instance Method Details

#apply_hash_colors(level, str) ⇒ Object



31
32
33
34
35
36
# File 'lib/rsmp/log/colorization.rb', line 31

def apply_hash_colors(level, str)
  colors = default_colors
  style = @settings['style']
  colors.merge!(style) if style.is_a?(Hash)
  colorize_with_map(level, str, colors)
end

#colorize(level, str) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rsmp/log/colorization.rb', line 38

def colorize(level, str)
  style = @settings['style']
  return str if style == false || style.nil?

  if style == true || style.is_a?(Hash)
    apply_hash_colors(level, str)
  elsif %i[nack warning error].include?(level)
    str.colorize(style).bold
  else
    str.colorize style
  end
end

#colorize_with_map(level, str, colors) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rsmp/log/colorization.rb', line 17

def colorize_with_map(level, str, colors)
  color = colors[level.to_s]
  return str unless color

  if color.is_a?(Hash)
    opts = {}
    opts[:color] = color['color'].to_sym if color['color']
    opts[:mode] = color['mode'].to_sym if color['mode']
    str.colorize(opts)
  else
    str.colorize(color.to_sym)
  end
end

#default_colorsObject



5
6
7
8
9
10
11
12
13
14
15
# File 'lib/rsmp/log/colorization.rb', line 5

def default_colors
  {
    'error' => { 'color' => 'red' },
    'warning' => { 'color' => 'yellow' },
    'info' => { 'color' => 'white' },
    'log' => { 'color' => 'white', 'mode' => 'dim' },
    'statistics' => { 'color' => 'grey', 'mode' => 'dim' },
    'debug' => { 'color' => 'grey', 'mode' => 'dim' },
    'collect' => { 'color' => 'grey', 'mode' => 'dim' }
  }
end