From 05b65b5212679a854a5a649401641d9c62f955d4 Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 12 Jun 2024 22:48:00 +0400 Subject: [PATCH] ansi_common --- lib/formatters/ansi_common.rb | 12 ++++++++++++ lib/formatters/ansi_formatter.rb | 12 +++++------- 2 files changed, 17 insertions(+), 7 deletions(-) create mode 100644 lib/formatters/ansi_common.rb diff --git a/lib/formatters/ansi_common.rb b/lib/formatters/ansi_common.rb new file mode 100644 index 0000000..15646a5 --- /dev/null +++ b/lib/formatters/ansi_common.rb @@ -0,0 +1,12 @@ +require_relative 'ansi_colors' + +# gather common definitions and functions +class AnsiCommon + def self.ansi_trace(trace) + match = trace.match(/(↳ )?(.*:\d+):in `(.*)'/) + return trace unless match + + _, m2, m3 = match.captures + "➟ #{m2} #{AnsiColors::BOLD}#{m3}#{AnsiColors::CLEAR}" + end +end diff --git a/lib/formatters/ansi_formatter.rb b/lib/formatters/ansi_formatter.rb index 2aa4db7..2daf463 100644 --- a/lib/formatters/ansi_formatter.rb +++ b/lib/formatters/ansi_formatter.rb @@ -1,6 +1,7 @@ require_relative 'ansi_wrapper' require_relative 'ansi_colors' require_relative 'ansi_dimensions' +require_relative 'ansi_common' require 'io/console' require 'amazing_print' @@ -81,6 +82,9 @@ class AnsiFormatter < SemanticLogger::Formatters::Color elsif log.name =~ /^(ActionView|ActiveRecord)::Base/ log.level = :debug log.message.lstrip! + if log.name == 'ActiveRecord::Base' && log.message.starts_with?('↳ ') + log.message = AnsiCommon.ansi_trace(log.message) + end end log @@ -99,13 +103,7 @@ class AnsiFormatter < SemanticLogger::Formatters::Color def stackisize(items) return '' if items.empty? - traces = items.map do |item| - trace = item.match(/(.*:\d+):in `(.*)'/) - next item unless trace - - m1, m2 = trace.captures - "↳ #{m1} #{AnsiColors::BOLD}#{m2}#{AnsiColors::CLEAR}" - end + traces = items.map { |item| AnsiCommon.ansi_trace(item) } "\n#{traces.join("\n")}" end