From a0b058b36e188119d1d3906ca5bed2e592e8b010 Mon Sep 17 00:00:00 2001 From: pvincent Date: Fri, 5 Jan 2024 23:12:56 +0400 Subject: [PATCH] refactored --- app/controllers/scores_controller.rb | 4 +--- lib/formatters/basic_formatter.rb | 34 +++++++++++++++------------- 2 files changed, 19 insertions(+), 19 deletions(-) diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index 3d97497..52f6e56 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -14,9 +14,7 @@ class ScoresController < ApplicationController end # GET /scores/1 - def show - puts "show #{params}" - end + def show; end # GET /scores/new def new diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index 16ce238..8cb065f 100644 --- a/lib/formatters/basic_formatter.rb +++ b/lib/formatters/basic_formatter.rb @@ -1,4 +1,4 @@ -# My Custom colorized formatter +# Opinioned Rails custom formatter class BasicFormatter < SemanticLogger::Formatters::Color NAME_MAX_SIZE = 25 @@ -12,18 +12,28 @@ class BasicFormatter < SemanticLogger::Formatters::Color ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze ANSI_REVERSED_FATAL = "\e[1;30;41m".freeze + CONTENT_COLOR_MAP = ColorMap.new( + debug: ANSI_DEBUG, + info: ANSI_NEUTRAL_INFO, + warn: ANSI_REVERSED_WARNING, + error: ANSI_REVERSED_ERROR, + fatal: ANSI_REVERSED_FATAL + ) + def initialize - super(time_format: nil, - color_map: ColorMap.new(debug: ANSI_DEBUG, info: ANSI_INFO, warn: ANSI_WARN, error: ANSI_ERROR, - fatal: ANSI_ERROR)) - @content_color_map = ColorMap.new(debug: ANSI_DEBUG, info: ANSI_NEUTRAL_INFO, warn: ANSI_REVERSED_WARNING, - error: ANSI_REVERSED_ERROR, fatal: ANSI_REVERSED_FATAL) + super(color_map: ColorMap.new( + debug: ANSI_DEBUG, + info: ANSI_INFO, + warn: ANSI_WARN, + error: ANSI_ERROR, + fatal: ANSI_ERROR + )) end def message return unless log.message - " #{@content_color_map[log.level]}#{log.message}#{color_map.clear}" + " #{CONTENT_COLOR_MAP[log.level]}#{log.message}#{color_map.clear}" end def level @@ -35,19 +45,11 @@ class BasicFormatter < SemanticLogger::Formatters::Color "#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}" end - def process_info - fname = file_name_and_line - "#{color}[#{fname}]#{color_map.clear}" if fname - end - def exception return unless log.exception root_path = Rails.root.to_s - backtrace = log.exception.backtrace.select do |line| - line.starts_with?(root_path) - end - + 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