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.
 
 
 
 
 

78 lines
2.3 KiB

module Semantic
# Abstract colorized formatter
class AbstractFormatter < SemanticLogger::Formatters::Color
include AnsiColors
TAG_NONE = ''.freeze
CENTER_SIZE = 20
TAG_FIXED_LENGTH = Rails.application.config.x.action_controller.main_session_tag_fixed_length || 10
def initialize(time_format: nil)
super(color_map: ColorMap.new(
debug: CLEAR + TEXT_GRAY_400,
info: CLEAR + TEXT_GRAY_100,
warn: CLEAR + BG_YELLOW + TEXT_BLACK,
error: CLEAR + DARK_BG_RED + TEXT_WHITE,
fatal: CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE
))
@time_format = time_format
end
private
def exception
return unless log.exception
if log.exception.is_a?(Semantic::TagWrapError)
exc_wrapper = log.exception
# puts "TAG_WRAP detected #{exc_wrapper.tag}"
exc = exc_wrapper.error
log.tags = Array.wrap(exc_wrapper.tag)
else
exc = log.exception
end
clazz = colorize("#{exc.class}\n", color_map[:fatal])
message = colorize(exc.message.chomp(''), color_map[:error])
# TODO: backtrace_cleaner might be optionally disable from Live::Constant
backtrace = stackisize(Rails.backtrace_cleaner.clean(exc.backtrace))
"#{clazz}#{message}#{backtrace}"
end
def origin
true_class = log.name == log.name.upcase_first
taint = true_class ? TEXT_CYAN : TEXT_GRAY_400
colorize(centerize(log.name), taint)
end
def ansi_trace(trace, symbol)
match = trace.match(/(↳ )?(.*:\d+)(:in `)?(.*'?)/) # only m2(=file) and m4(=optional function) are useful
return trace unless match
_, m2, _, m4 = match.captures
"#{symbol} #{m2} #{BOLD}#{m4.chop}#{CLEAR}"
end
def stackisize(items)
return '' if items.empty?
traces = items.map { |item| ansi_trace(item, '➟') }
"\n#{traces.join("\n")}"
end
def tags
first_tag, taint = log.tags.empty? ? [TAG_NONE, CLEAR] : [log.tags.first, BG_GRAY + TEXT_YELLOW]
colorize(centerize(first_tag, TAG_FIXED_LENGTH), taint)
end
def named_tags
return if log.named_tags.empty?
log.named_tags
end
def centerize(text, max_length = CENTER_SIZE) = text.reverse.truncate(max_length).reverse.center(max_length)
end
end