|
@ -5,14 +5,16 @@ require 'io/console' |
|
|
require 'amazing_print' |
|
|
require 'amazing_print' |
|
|
require 'json' |
|
|
require 'json' |
|
|
|
|
|
|
|
|
|
|
|
# wraps meanwhile takes care of ansi colors |
|
|
class AnsiFormatter < SemanticLogger::Formatters::Color |
|
|
class AnsiFormatter < SemanticLogger::Formatters::Color |
|
|
include AnsiColors |
|
|
include AnsiColors |
|
|
|
|
|
|
|
|
ANSI_DEBUG = TEXT_GRAY_400 |
|
|
|
|
|
ANSI_INFO = TEXT_GRAY_100 |
|
|
|
|
|
ANSI_WARN = BG_YELLOW + TEXT_BLACK |
|
|
|
|
|
ANSI_ERROR = BG_RED + TEXT_BLACK |
|
|
|
|
|
ANSI_FATAL = DARK_BG_RED + TEXT_BLACK |
|
|
|
|
|
|
|
|
ANSI_DEBUG = CLEAR + TEXT_GRAY_400 |
|
|
|
|
|
ANSI_INFO = CLEAR + TEXT_GRAY_100 |
|
|
|
|
|
ANSI_WARN = CLEAR + BG_YELLOW + TEXT_BLACK |
|
|
|
|
|
ANSI_ERROR = CLEAR + BG_RED + TEXT_WHITE |
|
|
|
|
|
ANSI_FATAL = CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE |
|
|
|
|
|
NAME_MAX_SIZE = 20 |
|
|
|
|
|
|
|
|
def initialize |
|
|
def initialize |
|
|
super(color_map: ColorMap.new( |
|
|
super(color_map: ColorMap.new( |
|
@ -24,12 +26,6 @@ class AnsiFormatter < SemanticLogger::Formatters::Color |
|
|
)) |
|
|
)) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def message |
|
|
|
|
|
return unless log.message |
|
|
|
|
|
|
|
|
|
|
|
colorize(log.message, color_map[log.level]) |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def call(log, logger) |
|
|
def call(log, logger) |
|
|
self.log = log |
|
|
self.log = log |
|
|
self.logger = logger |
|
|
self.logger = logger |
|
@ -37,15 +33,43 @@ class AnsiFormatter < SemanticLogger::Formatters::Color |
|
|
wrap_level(level, message, payload, exception) |
|
|
wrap_level(level, message, payload, exception) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def origin |
|
|
|
|
|
origin = log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE) |
|
|
|
|
|
colorize(origin, AnsiColors::TEXT_CYAN) |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def message |
|
|
|
|
|
return unless log.message |
|
|
|
|
|
|
|
|
|
|
|
colorize(log.message) |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def exception |
|
|
|
|
|
return unless log.exception |
|
|
|
|
|
|
|
|
|
|
|
clazz = "#{log.exception.class}\n" |
|
|
|
|
|
message = log.exception.message.chomp('') |
|
|
|
|
|
backtrace = clean_backtrace(log.exception.backtrace) |
|
|
|
|
|
|
|
|
|
|
|
"#{colorize(clazz, ANSI_FATAL)}#{colorize(message, ANSI_ERROR)}#{backtrace}" |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
private |
|
|
private |
|
|
|
|
|
|
|
|
def colorize(text, color) |
|
|
|
|
|
"#{color}#{text}#{color_map.clear}" |
|
|
|
|
|
|
|
|
def clean_backtrace(backtrace) |
|
|
|
|
|
root_path = Rails.root.to_s |
|
|
|
|
|
backtrace = backtrace.select { |line| line.starts_with?(root_path) } |
|
|
|
|
|
backtrace = backtrace.map { |line| line.delete_prefix("#{root_path}/") } |
|
|
|
|
|
backtrace.compact.join("\n") |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
|
|
|
def colorize(text, tint = color) |
|
|
|
|
|
"#{tint}#{text}#{AnsiColors::CLEAR}" |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def wrap_level(level, *items) |
|
|
def wrap_level(level, *items) |
|
|
prefix = " #{colorize('>', color)} " |
|
|
|
|
|
continuation = " #{colorize('#', color)} " |
|
|
|
|
|
|
|
|
prefix = "#{origin} #{colorize('>')} " |
|
|
|
|
|
continuation = "#{origin} #{colorize('#')} " |
|
|
items.map do |item| |
|
|
items.map do |item| |
|
|
AnsiWrapper.wrap(item, 100, prefix, continuation) |
|
|
AnsiWrapper.wrap(item, 100, prefix, continuation) |
|
|
end.compact.join("\n") # FIXME: previously was: join(' ') |
|
|
end.compact.join("\n") # FIXME: previously was: join(' ') |
|
|