Browse Source

refactor3

pagy
pvincent 10 months ago
parent
commit
482a53d6b3
  1. 1
      app/controllers/scores_controller.rb
  2. 24
      lib/formatters/basic_formatter.rb

1
app/controllers/scores_controller.rb

@ -10,6 +10,7 @@ class ScoresController < ApplicationController
# logger.info 'this is an information', { four: 4, five: 5 } # logger.info 'this is an information', { four: 4, five: 5 }
# logger.debug BigDecimal('0.0003') # logger.debug BigDecimal('0.0003')
# logger.warn 'scores are', @scores # logger.warn 'scores are', @scores
logger.warn 'this is a warning'
logger.error 'this is an error message' logger.error 'this is an error message'
logger.info 'end of normal message' logger.info 'end of normal message'
# logger.debug @scores # logger.debug @scores

24
lib/formatters/basic_formatter.rb

@ -63,7 +63,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
return unless log.message return unless log.message
message = wrap_message(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 end
def payload def payload
@ -88,7 +88,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
end end
def name 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 end
def exception # rubocop:disable Metrics/AbcSize 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 = log.exception.backtrace.select { |line| line.starts_with?(root_path) }
stack = stack.map { |line| line.delete_prefix("#{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 end
def call(log, logger) def call(log, logger)
@ -111,8 +112,12 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
private private
def ansi_wrap(text, ansi_code)
"#{ansi_code}#{text}#{color_map.clear}"
end
def draw_rails(char) def draw_rails(char)
"#{color}#{char}#{color_map.clear}"
ansi_wrap("#{char}", color)
end end
def continuation def continuation
@ -147,7 +152,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
case message case message
when /^Completed/ then transform_rails_completed(message) when /^Completed/ then transform_rails_completed(message)
when /^Started/ then two_captures_last_as_bold(message, /(^Started \w* )"(.*?)"/) 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) when /^Processing/ then transform_rails_processing(message)
else message end else message end
end end
@ -165,7 +170,8 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
def transform_rails_completed(message) def transform_rails_completed(message)
m1, m2, m3, m4 = message.match(/^Completed (\d*) (.*) in (\d*)ms(.*)$/).captures 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 += m4 if m3.to_i > COMPLETED_DURATION
message += "\n" if m1 =~ /^[23]/ message += "\n" if m1 =~ /^[23]/
message message
@ -173,7 +179,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
def two_captures_last_as_bold(message, regex) def two_captures_last_as_bold(message, regex)
m1, m2 = message.match(regex).captures m1, m2 = message.match(regex).captures
"#{m1}#{ANSI_BOLD}#{m2}#{ANSI_RESET}"
"#{m1}#{ansi_wrap(m2, ANSI_BOLD)}"
end end
def transform_action_view_base(log) 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" "\n#{before_message}#{ANSI_ERROR}#{PREFIX_BUG_INTERNAL}#{stack.join("\n#{before_message}#{ANSI_ERROR}#{PREFIX_BUG_INTERNAL}")}#{color_map.clear}\n"
end end
def color_content
color
end
end end
Loading…
Cancel
Save