Class: ExtendedLogger::Formatter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#logger_formatterObject



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

def logger_formatter
  @logger_formatter ||= Logger::Formatter.new
end

#paletteObject



51
52
53
# File 'lib/extended_logger/formatter.rb', line 51

def palette
  @palette ||= {}
end

Class Method Details

.ansi_colorsObject



55
56
57
# File 'lib/extended_logger/formatter.rb', line 55

def self.ansi_colors
  @ansi_colors ||= i(black red green yellow blue magenta cyan white)
end

.col(fg, brightness, bg = nil) ⇒ Object



59
60
61
62
63
64
65
66
67
68
# File 'lib/extended_logger/formatter.rb', line 59

def self.col fg, brightness, bg=nil
  brightness = { :bright => 1, :normal => 0 }.fetch brightness
  escape = "\e[#{brightness};3#{ansi_colors.index fg}m"

  if bg
    escape << "\e[4#{ansi_colors.index bg}m"
  end

  escape
end

.default_paletteObject



70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# File 'lib/extended_logger/formatter.rb', line 70

def self.default_palette
  @default_palette ||= {
    'OBSOLETE' => col(:black, :normal),
    'DATA' => col(:green, :normal),
    'TRACE' => col(:cyan, :normal),
    'DEBUG' => col(:blue, :normal),
    'OPT_DATA' => col(:green, :normal, :white),
    'OPT_TRACE' => col(:cyan, :normal, :white),
    'OPT_DEBUG' => col(:blue, :normal, :white),
    'PASS' => col(:white, :bright, :green),
    'FAIL' => col(:white, :bright, :red),
    'FOCUS' => col(:white, :bright, :blue),
    'WARN' => col(:yellow, :normal),
    'ERROR' => col(:red, :normal),
    'FATAL' => col(:black, :bright, :red),
    'ANY' => col(:white, :bright, :magenta),
  }
end

Instance Method Details

#call(severity, *arguments) ⇒ Object



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

def call severity, *arguments
  log_entry = delegate severity, *arguments
  color log_entry, severity
end

#color(log_entry, severity) ⇒ Object



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

def color log_entry, severity
  colorizer = palette[severity]

  if colorizer
    log_entry = "#{colorizer}#{log_entry}\e[0m"
  end

  log_entry
end

#delegate(*arguments, message) ⇒ Object



21
22
23
24
# File 'lib/extended_logger/formatter.rb', line 21

def delegate *arguments, message
  message = format_message message, arguments
  logger_formatter.(*arguments, message)
end

#format_message(message, arguments) ⇒ Object



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/extended_logger/formatter.rb', line 26

def format_message message, arguments
  output = ''

  message.each_line "\n".freeze do |line|
    if line == "\n".freeze
      line = "\\n"
    else
      line.gsub! "\r".freeze, "\\r".freeze
    end

    if output.empty?
      output.concat line
    else
      formatted_line = logger_formatter.(*arguments, line.chomp("\n".freeze))
      output.concat formatted_line
    end
  end

  output.chomp "\n".freeze
end