From 482a53d6b3405a847e4d962e1da9dd90e6684540 Mon Sep 17 00:00:00 2001 From: pvincent Date: Wed, 24 Jan 2024 11:36:03 +0400 Subject: [PATCH] refactor3 --- app/controllers/scores_controller.rb | 1 + lib/formatters/basic_formatter.rb | 24 +++++++++++++----------- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index d9a597a..4e36c93 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -10,6 +10,7 @@ class ScoresController < ApplicationController # logger.info 'this is an information', { four: 4, five: 5 } # logger.debug BigDecimal('0.0003') # logger.warn 'scores are', @scores + logger.warn 'this is a warning' logger.error 'this is an error message' logger.info 'end of normal message' # logger.debug @scores diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index 144ee8b..ef119d2 100644 --- a/lib/formatters/basic_formatter.rb +++ b/lib/formatters/basic_formatter.rb @@ -63,7 +63,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri return unless log.message message = wrap_message(log.message) - "#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}" + ansi_wrap(message, CONTENT_COLOR_MAP[log.level]) end def payload @@ -88,7 +88,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri end def name - "#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}" + ansi_wrap(log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE), ANSI_DEBUG) end def exception # rubocop:disable Metrics/AbcSize @@ -98,7 +98,8 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri stack = log.exception.backtrace.select { |line| line.starts_with?(root_path) } stack = stack.map { |line| line.delete_prefix("#{root_path}/") } - "#{ANSI_REVERSED_WARNING}#{log.exception.class}#{color_map.clear} #{ANSI_REVERSED_ERROR}#{log.exception.message}#{color_map.clear}#{backtrace(stack)}" # rubocop:disable Layout/LineLength + "#{ansi_wrap(log.exception.class, + ANSI_REVERSED_WARNING)} #{ansi_wrap(log.exception.message, ANSI_REVERSED_ERROR)}#{backtrace(stack)}" end def call(log, logger) @@ -111,8 +112,12 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri private + def ansi_wrap(text, ansi_code) + "#{ansi_code}#{text}#{color_map.clear}" + end + def draw_rails(char) - "#{color}╣#{char}╠#{color_map.clear}" + ansi_wrap("╣#{char}╠", color) end def continuation @@ -147,7 +152,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri case message when /^Completed/ then transform_rails_completed(message) when /^Started/ then two_captures_last_as_bold(message, /(^Started \w* )"(.*?)"/) - when / Parameters/ then transform_rails_parameters(message) + when /^ Parameters/ then transform_rails_parameters(message) when /^Processing/ then transform_rails_processing(message) else message end end @@ -165,7 +170,8 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri def transform_rails_completed(message) m1, m2, m3, m4 = message.match(/^Completed (\d*) (.*) in (\d*)ms(.*)$/).captures - message = "Completed #{ANSI_BOLD}#{m1} #{m2}#{ANSI_RESET} in #{m3}ms" + http_code = ansi_wrap("#{m1} #{m2}", ANSI_BOLD) + message = "Completed #{http_code} in #{m3}ms" message += m4 if m3.to_i > COMPLETED_DURATION message += "\n" if m1 =~ /^[23]/ message @@ -173,7 +179,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri def two_captures_last_as_bold(message, regex) m1, m2 = message.match(regex).captures - "#{m1}#{ANSI_BOLD}#{m2}#{ANSI_RESET}" + "#{m1}#{ansi_wrap(m2, ANSI_BOLD)}" end def transform_action_view_base(log) @@ -229,8 +235,4 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri "\n#{before_message}#{ANSI_ERROR}#{PREFIX_BUG_INTERNAL}#{stack.join("\n#{before_message}#{ANSI_ERROR}#{PREFIX_BUG_INTERNAL}")}#{color_map.clear}\n" end - - def color_content - color - end end