From 6bf1bbead39187ce9f31d259dd1e2091741ef1d0 Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 24 Jan 2024 11:44:19 +0400 Subject: [PATCH] refactor4 --- lib/formatters/basic_formatter.rb | 17 ++++++++++------- 1 file changed, 10 insertions(+), 7 deletions(-) diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index ef119d2..8a3a492 100644 --- a/lib/formatters/basic_formatter.rb +++ b/lib/formatters/basic_formatter.rb @@ -91,15 +91,13 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri ansi_wrap(log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE), ANSI_DEBUG) end - def exception # rubocop:disable Metrics/AbcSize + def exception return unless log.exception - root_path = Rails.root.to_s - stack = log.exception.backtrace.select { |line| line.starts_with?(root_path) } - stack = stack.map { |line| line.delete_prefix("#{root_path}/") } + backtrace = backtrace(log.exception) "#{ansi_wrap(log.exception.class, - ANSI_REVERSED_WARNING)} #{ansi_wrap(log.exception.message, ANSI_REVERSED_ERROR)}#{backtrace(stack)}" + ANSI_REVERSED_WARNING)} #{ansi_wrap(log.exception.message, ANSI_REVERSED_ERROR)}#{backtrace}" end def call(log, logger) @@ -230,9 +228,14 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri [name, wrapped ? continuation : level, tags, named_tags, duration].compact.join(' ') + CONTENT_PREFIX end - def backtrace(stack) + def backtrace(exception) + root_path = Rails.root.to_s + stack = exception.backtrace.select { |line| line.starts_with?(root_path) } + stack = stack.map { |line| line.delete_prefix("#{root_path}/") } return "\n" unless stack.count.positive? - "\n#{before_message}#{ANSI_ERROR}#{PREFIX_BUG_INTERNAL}#{stack.join("\n#{before_message}#{ANSI_ERROR}#{PREFIX_BUG_INTERNAL}")}#{color_map.clear}\n" + stack_message = PREFIX_BUG_INTERNAL + stack_message += stack.join("\n#{before_message}#{ANSI_ERROR}#{PREFIX_BUG_INTERNAL}") + "\n#{before_message}#{ansi_wrap(stack_message, ANSI_ERROR)}\n" end end