class ExtendedLogger::Formatter

Attributes

logger_formatter[W]
palette[W]

Public Class Methods

ansi_colors() click to toggle source
# 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) click to toggle source
# 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_palette() click to toggle source
# 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

Public Instance Methods

call(severity, *arguments) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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) click to toggle source
# 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
logger_formatter() click to toggle source
# File lib/extended_logger/formatter.rb, line 47
def logger_formatter
  @logger_formatter ||= Logger::Formatter.new
end
palette() click to toggle source
# File lib/extended_logger/formatter.rb, line 51
def palette
  @palette ||= {}
end