From c7e37f4a4ac84218c58606f2ad72a35723531c68 Mon Sep 17 00:00:00 2001 From: pvincent Date: Thu, 18 Jan 2024 18:11:42 +0400 Subject: [PATCH] VIEW_RENDERED_DURATION --- app/controllers/scores_controller.rb | 6 ++-- lib/formatters/base.rb | 6 ++-- lib/formatters/basic_formatter.rb | 47 +++++++++++++++++++++------- 3 files changed, 43 insertions(+), 16 deletions(-) diff --git a/app/controllers/scores_controller.rb b/app/controllers/scores_controller.rb index d38ab60..8701093 100644 --- a/app/controllers/scores_controller.rb +++ b/app/controllers/scores_controller.rb @@ -8,9 +8,9 @@ class ScoresController < ApplicationController @scores = Score.all logger.debug 'this is a debug message' logger.debug({ one: 1, two: 2 }) - logger.info 'this is an information', { four: 4, five: 5 } - logger.debug BigDecimal('0.0003') - logger.debug 'scores are', @scores + # logger.info 'this is an information', { four: 4, five: 5 } + # logger.debug BigDecimal('0.0003') + # logger.debug 'scores are', @scores # logger.debug @scores end diff --git a/lib/formatters/base.rb b/lib/formatters/base.rb index bfbf9c4..51b0614 100644 --- a/lib/formatters/base.rb +++ b/lib/formatters/base.rb @@ -1,7 +1,9 @@ +# MonkeyPatch of SemanticLogger +# provides better handling for payload regardless of message module SemanticLogger - class Base + class Base # rubocop:disable Style/Documentation # Log message at the specified level - def log_internal(level, index, message = nil, payload = nil, exception = nil) + def log_internal(level, index, message = nil, payload = nil, exception = nil) # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity # Handle variable number of arguments by detecting exception object and payload hash. if exception.nil? && payload.nil? && message.respond_to?(:backtrace) && message.respond_to?(:message) exception = message diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index 29e919b..1e9d32d 100644 --- a/lib/formatters/basic_formatter.rb +++ b/lib/formatters/basic_formatter.rb @@ -9,6 +9,8 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri TERMINAL_PREFIX = ENV['TERMINAL_PREFIX'].to_i || 0 CONTENT_PREFIX = ' '.freeze + RENDERED_VIEW_DURATION = 100 + ANSI_DEBUG = "\e[90m".freeze ANSI_INFO = SemanticLogger::AnsiColors::GREEN ANSI_WARN = SemanticLogger::AnsiColors::YELLOW @@ -46,7 +48,6 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri error: ANSI_ERROR, fatal: ANSI_ERROR )) - @caller = nil end def message @@ -77,14 +78,6 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri end end - def draw_rails(char) - "#{color}╣#{char}╠#{color_map.clear}" - end - - def continuation - draw_rails('┆') - end - def name "#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}" end @@ -109,10 +102,17 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri private + def draw_rails(char) + "#{color}╣#{char}╠#{color_map.clear}" + end + + def continuation + draw_rails('┆') + end + def transform_log(log) if log.name == 'ActionView::Base' - log.level = :debug - log.message = log.message.lstrip + log = transform_action_view_base(log) elsif log.name == 'Rails' && log.message message = log.message.rstrip message += "\n" if message.starts_with?('Completed 2') || message.starts_with?('Completed 3') @@ -121,6 +121,31 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri log end + def transform_action_view_base(log) + log.level, message = transform_log_debug_lstrip(log) + message = transform_rendered_message_with_filename(message) + message = transform_duration_strip(message) + log.message = message + log + end + + def transform_duration_strip(message) + md2 = message.match(/( \(Duration: \d+\.?\d?ms.*\))/) + md3 = md2.match(1).match(/Duration: (\d+\.?\d?)ms/) + duration = md3.match(1).to_f + duration < RENDERED_VIEW_DURATION ? md2.pre_match : message + end + + def transform_rendered_message_with_filename(message) + md = message.match(/^Rendered( layout| collection of|) (.*?\.erb)/) + filename = "app/views/#{md.match(2)}" + log.message = "Rendered#{md.match(1)} #{filename}#{md.post_match}" + end + + def transform_log_debug_lstrip(log) + [:debug, log.message.lstrip] + end + def wrap_message(message) message, space_prefix = split_spaces_in_front(message) message = Wrapper.wrap("#{CONTENT_COLOR_MAP[log.level]}#{message}",