You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
101 lines
2.7 KiB
101 lines
2.7 KiB
require_relative 'ansi_wrapper'
|
|
require_relative 'ansi_colors'
|
|
|
|
require 'io/console'
|
|
require 'amazing_print'
|
|
require 'json'
|
|
|
|
# wraps meanwhile takes care of ansi colors
|
|
class AnsiFormatter < SemanticLogger::Formatters::Color
|
|
include AnsiColors
|
|
|
|
CENTER_SIZE = 20
|
|
FOREMAN_PREFIX_LENGTH = 18
|
|
FAILOVER_WRAP = 80
|
|
|
|
def initialize
|
|
super(color_map:
|
|
ColorMap.new(
|
|
debug: CLEAR + TEXT_GRAY_400,
|
|
info: CLEAR + TEXT_GRAY_100,
|
|
warn: CLEAR + BG_YELLOW + TEXT_BLACK,
|
|
error: CLEAR + BG_RED + TEXT_WHITE,
|
|
fatal: CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE
|
|
))
|
|
end
|
|
|
|
def call(log, logger)
|
|
log = AnsiFilter.alter(log)
|
|
|
|
self.log = log
|
|
self.logger = logger
|
|
self.color = color_map[log.level]
|
|
|
|
wrap_level(compute_useful_length, message, payload, exception)
|
|
end
|
|
|
|
private
|
|
|
|
def origin = colorize(centerize(log.name), TEXT_CYAN)
|
|
def build_prefix(char) = "#{origin} ╣#{colorize(char)}╠ "
|
|
def build_terminus = "#{origin} #{AnsiFilter::TERMINUS_STRING} "
|
|
def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
|
|
def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
|
|
def stackisize(items) = items.length.positive? ? "\n#{items.join("\n")}" : ''
|
|
|
|
def build_dimensions(dimensions)
|
|
"#{origin} #{dimensions.rails} "
|
|
end
|
|
|
|
def compute_useful_length
|
|
IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
|
|
rescue StandardError
|
|
FAILOVER_WRAP
|
|
end
|
|
|
|
def message
|
|
colorize(log.message) if log.message
|
|
end
|
|
|
|
def exception
|
|
return unless log.exception
|
|
|
|
exc = log.exception
|
|
clazz = colorize("#{exc.class}\n", color_map[:fatal])
|
|
message = colorize(exc.message.chomp(''), color_map[:error])
|
|
backtrace = stackisize(clean_backtrace(exc.backtrace))
|
|
|
|
"#{clazz}#{message}#{backtrace}"
|
|
end
|
|
|
|
def clean_backtrace(backtrace)
|
|
root_path = Rails.root.to_s
|
|
backtrace = backtrace.select { |line| line.starts_with?(root_path) }
|
|
backtrace.map { |line| line.delete_prefix("#{root_path}/") }
|
|
end
|
|
|
|
def level_char
|
|
case log.level
|
|
when :info, :debug then ' '
|
|
else log.level.to_s.chr.upcase
|
|
end
|
|
end
|
|
|
|
def wrap_level(length, *items)
|
|
prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
|
|
continuation = build_prefix('┆')
|
|
|
|
result = items.map do |item|
|
|
AnsiWrapper.wrap(item, length, prefix, continuation)
|
|
end
|
|
|
|
if log.dimensions&.terminus
|
|
terminus = AnsiWrapper.wrap(' ', length, build_terminus)
|
|
result << terminus
|
|
end
|
|
|
|
log.dimensions&.before&.times { result.unshift('') }
|
|
log.dimensions&.after&.times { result << '' }
|
|
result.compact.join("\n")
|
|
end
|
|
end
|