Browse Source

abstract_formatter

main
pvincent 2 months ago
parent
commit
4b0c7f236d
  1. 40
      lib/semantic/abstract_formatter.rb
  2. 38
      lib/semantic/basic_formatter.rb
  3. 42
      lib/semantic/fancy_formatter.rb

40
lib/semantic/abstract_formatter.rb

@ -1,5 +1,45 @@
module Semantic module Semantic
# Abstract colorized formatter # Abstract colorized formatter
class AbstractFormatter < SemanticLogger::Formatters::Color class AbstractFormatter < SemanticLogger::Formatters::Color
include AnsiColors
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 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
end end
end end

38
lib/semantic/basic_formatter.rb

@ -53,41 +53,6 @@ module Semantic
"#{color}[#{fname}]#{color_map.clear}" if fname "#{color}[#{fname}]#{color_map.clear}" if fname
end end
def exception
return unless log.exception
root_path = Rails.root.to_s
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
backtrace = exc.backtrace.select do |line|
line.starts_with?(root_path)
end
if backtrace.count.positive?
"-- #{ANSI_REVERSED_ERROR}#{exc.class}#{color_map.clear} #{color}#{exc.message}#{color_map.clear}#{SemanticLogger::AnsiColors::WHITE}\n\t#{backtrace.join("\n\t")}#{color_map.clear}\n\n"
else
"-- #{ANSI_REVERSED_ERROR}#{exc.class}: #{exc.message}#{color_map.clear}\n\n"
end
# TODO: remove me
# 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 call(log, logger) def call(log, logger)
self.color = color_map[log.level] self.color = color_map[log.level]
self.log = log self.log = log
@ -99,6 +64,9 @@ module Semantic
else else
[tags, named_tags, duration, name, message, payload, exception].compact.join(' ') [tags, named_tags, duration, name, message, payload, exception].compact.join(' ')
end end
rescue StandardError => e
puts "Error during formatting: #{e.message}"
puts e.backtrace.join("\n")
end end
end end
end end

42
lib/semantic/fancy_formatter.rb

@ -5,8 +5,6 @@ require 'json'
module Semantic module Semantic
# wraps meanwhile takes care of ansi colors # wraps meanwhile takes care of ansi colors
class FancyFormatter < AbstractFormatter class FancyFormatter < AbstractFormatter
include AnsiColors
TAG_NONE = ''.freeze TAG_NONE = ''.freeze
CENTER_SIZE = 20 CENTER_SIZE = 20
FOREMAN_PREFIX_LENGTH = 18 FOREMAN_PREFIX_LENGTH = 18
@ -41,6 +39,9 @@ module Semantic
return result unless computed_exception return result unless computed_exception
result << "\n" << build_terminus result << "\n" << build_terminus
rescue StandardError => e
puts "Error during formatting: #{e.message}"
puts e.backtrace.join("\n")
end end
def reject(log) def reject(log)
@ -70,14 +71,6 @@ module Semantic
Regexp.new(regex_s) Regexp.new(regex_s)
end 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 two_captures_last_as_bold(message, regex) def two_captures_last_as_bold(message, regex)
match = message.match(regex) match = message.match(regex)
return "unmatched: #{message}" unless match return "unmatched: #{message}" unless match
@ -136,13 +129,6 @@ module Semantic
colorize(centerize(log.name), taint) colorize(centerize(log.name), taint)
end end
def stackisize(items)
return '' if items.empty?
traces = items.map { |item| ansi_trace(item, '➟') }
"\n#{traces.join("\n")}"
end
def build_dimensions(dimensions) def build_dimensions(dimensions)
"#{tags} #{origin} #{dimensions.rails} " "#{tags} #{origin} #{dimensions.rails} "
end end
@ -165,28 +151,6 @@ module Semantic
log.payload.ai(indent: 2, object_id: false) log.payload.ai(indent: 2, object_id: false)
end end
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 level_char def level_char
case log.level case log.level
when :info, :debug then ' ' when :info, :debug then ' '

Loading…
Cancel
Save