From eb675faa61c6f2dc4812839e98faeba258e4d02a Mon Sep 17 00:00:00 2001 From: pvincent Date: Sat, 6 Jan 2024 00:12:31 +0400 Subject: [PATCH] cute backtrace logging --- app/controllers/scores_controller.rb | 7 ++++++- lib/formatters/basic_formatter.rb | 29 ++++++++++++++++++++-------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 52f6e56..704b5c5 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -46,13 +46,18 @@ class ScoresController < ApplicationController # DELETE /scores/1 def destroy - raise 'Unable to destroy this score' + do_an_exception + @score.destroy! redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other end private + def do_an_exception + raise 'Unable to destroy this score' + end + # Use callbacks to share common setup or constraints between actions. def set_score @score = Score.find(params[:id]) diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index 8cb065f..511d4f8 100644 --- a/lib/formatters/basic_formatter.rb +++ b/lib/formatters/basic_formatter.rb @@ -1,6 +1,7 @@ # Opinioned Rails custom formatter class BasicFormatter < SemanticLogger::Formatters::Color NAME_MAX_SIZE = 25 + CONTENT_PREFIX = ' '.freeze ANSI_DEBUG = "\e[90m".freeze ANSI_INFO = SemanticLogger::AnsiColors::GREEN @@ -33,7 +34,12 @@ class BasicFormatter < SemanticLogger::Formatters::Color def message return unless log.message - " #{CONTENT_COLOR_MAP[log.level]}#{log.message}#{color_map.clear}" + message = log.message + if log.name == 'Rails' && message.starts_with?('Completed') + message.rstrip! + message += "\n" unless message.starts_with?('Completed 5') + end + "#{CONTENT_PREFIX}#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}" end def level @@ -45,16 +51,14 @@ class BasicFormatter < SemanticLogger::Formatters::Color "#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}" end - def exception + def exception # rubocop:disable Metrics/AbcSize return unless log.exception root_path = Rails.root.to_s - backtrace = log.exception.backtrace.select { |line| line.starts_with?(root_path) } - if backtrace.count.positive? - "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear} #{color}#{log.exception.message}\n#{color} ↳ #{backtrace.join("\n ↳ ")}#{color_map.clear}\n\n" - else - "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n" - end + 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 end def call(log, logger) @@ -64,4 +68,13 @@ class BasicFormatter < SemanticLogger::Formatters::Color [name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ') end + + private + + 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}" + end end