Browse Source

cute backtrace logging

pagy
pvincent 11 months ago
parent
commit
eb675faa61
  1. 7
      app/controllers/scores_controller.rb
  2. 29
      lib/formatters/basic_formatter.rb

7
app/controllers/scores_controller.rb

@ -46,13 +46,18 @@ class ScoresController < ApplicationController
# DELETE /scores/1 # DELETE /scores/1
def destroy def destroy
raise 'Unable to destroy this score'
do_an_exception
@score.destroy! @score.destroy!
redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other redirect_to scores_url, notice: 'Score was successfully destroyed.', status: :see_other
end end
private private
def do_an_exception
raise 'Unable to destroy this score'
end
# Use callbacks to share common setup or constraints between actions. # Use callbacks to share common setup or constraints between actions.
def set_score def set_score
@score = Score.find(params[:id]) @score = Score.find(params[:id])

29
lib/formatters/basic_formatter.rb

@ -1,6 +1,7 @@
# Opinioned Rails custom formatter # Opinioned Rails custom formatter
class BasicFormatter < SemanticLogger::Formatters::Color class BasicFormatter < SemanticLogger::Formatters::Color
NAME_MAX_SIZE = 25 NAME_MAX_SIZE = 25
CONTENT_PREFIX = ' '.freeze
ANSI_DEBUG = "\e[90m".freeze ANSI_DEBUG = "\e[90m".freeze
ANSI_INFO = SemanticLogger::AnsiColors::GREEN ANSI_INFO = SemanticLogger::AnsiColors::GREEN
@ -33,7 +34,12 @@ class BasicFormatter < SemanticLogger::Formatters::Color
def message def message
return unless log.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 end
def level 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}" "#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}"
end end
def exception
def exception # rubocop:disable Metrics/AbcSize
return unless log.exception return unless log.exception
root_path = Rails.root.to_s 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 end
def call(log, logger) def call(log, logger)
@ -64,4 +68,13 @@ class BasicFormatter < SemanticLogger::Formatters::Color
[name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ') [name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ')
end 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 end
Loading…
Cancel
Save