From d51b221cd0c462290a632c59f20f530982dd22e6 Mon Sep 17 00:00:00 2001 From: pvincent Date: Mon, 8 Jan 2024 21:21:33 +0400 Subject: [PATCH] before_message --- lib/formatters/basic_formatter.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index 9f4accf..41c8f89 100644 --- a/lib/formatters/basic_formatter.rb +++ b/lib/formatters/basic_formatter.rb @@ -48,7 +48,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color message.rstrip! message += "\n" unless message.starts_with?('Completed 5') end - "#{CONTENT_PREFIX}#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}" + "#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}" end def level @@ -67,22 +67,25 @@ class BasicFormatter < SemanticLogger::Formatters::Color stack = log.exception.backtrace.select { |line| line.starts_with?(root_path) } stack = stack.map { |line| line.delete_prefix("#{root_path}/") } - "#{CONTENT_PREFIX}#{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear}: #{color}#{log.exception.message}#{color_map.clear}#{backtrace(stack)}" # rubocop:disable Layout/LineLength + "#{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear}: #{color}#{log.exception.message}#{color_map.clear}#{backtrace(stack)}" # rubocop:disable Layout/LineLength end def call(log, logger) self.color = color_map[log.level] self.log = log self.logger = logger - [name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ') + [before_message, message, payload, exception].compact.join(' ') end private + def before_message + [name, level, tags, named_tags, duration, CONTENT_PREFIX].compact.join(' ') + end + def backtrace(stack) nil unless stack.count.positive? - prefix = [name, level, tags, named_tags, duration, message, payload, CONTENT_PREFIX].compact.join(' ') - "\n#{prefix}#{ANSI_ERROR}↳ #{stack.join("\n#{prefix}#{ANSI_ERROR}↳ ")}#{color_map.clear}" + "\n#{before_message} #{ANSI_ERROR}↳ #{stack.join("\n#{before_message} #{ANSI_ERROR}↳ ")}#{color_map.clear}" end end