Class: ExtendedLogger::ColoredFormatter

Inherits:
Formatter
  • Object
show all
Defined in:
lib/extended_logger/colored_formatter.rb

Constant Summary collapse

ANSI_COLORS =
i(black red green yellow blue magenta cyan white)

Instance Attribute Summary collapse

Attributes inherited from Formatter

#logger_formatter

Instance Method Summary collapse

Methods inherited from Formatter

#delegate, #format_message

Instance Attribute Details

#paletteObject



47
48
49
# File 'lib/extended_logger/colored_formatter.rb', line 47

def palette
  @palette || default_palette
end

Instance Method Details

#bg(*args) ⇒ Object



7
8
9
# File 'lib/extended_logger/colored_formatter.rb', line 7

def bg *args
  col :bg, *args
end

#call(*arguments) ⇒ Object



11
12
13
14
15
16
17
18
19
20
# File 'lib/extended_logger/colored_formatter.rb', line 11

def call *arguments
  log_entry = delegate *arguments

  severity = arguments.fetch 0
  colorizer = palette.fetch severity do
    -> message { message }
  end

  instance_exec log_entry, &colorizer
end

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



22
23
24
25
26
27
# File 'lib/extended_logger/colored_formatter.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

#default_paletteObject



33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/extended_logger/colored_formatter.rb', line 33

def default_palette
  @@default_palette ||= {
    'FOCUS' => -> msg { bg(:blue, :normal, fg(:white, :bright, msg)) },
    'ANY' => -> msg { bg(:cyan, :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) }
  }
end

#fg(*args) ⇒ Object



29
30
31
# File 'lib/extended_logger/colored_formatter.rb', line 29

def fg *args
  col :fg, *args
end