Browse Source

VIEW_RENDERED_DURATION

pagy
pvincent 10 months ago
parent
commit
c7e37f4a4a
  1. 6
      app/controllers/scores_controller.rb
  2. 6
      lib/formatters/base.rb
  3. 47
      lib/formatters/basic_formatter.rb

6
app/controllers/scores_controller.rb

@ -8,9 +8,9 @@ class ScoresController < ApplicationController
@scores = Score.all @scores = Score.all
logger.debug 'this is a debug message' logger.debug 'this is a debug message'
logger.debug({ one: 1, two: 2 }) 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 # logger.debug @scores
end end

6
lib/formatters/base.rb

@ -1,7 +1,9 @@
# MonkeyPatch of SemanticLogger
# provides better handling for payload regardless of message
module SemanticLogger module SemanticLogger
class Base
class Base # rubocop:disable Style/Documentation
# Log message at the specified level # 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. # 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) if exception.nil? && payload.nil? && message.respond_to?(:backtrace) && message.respond_to?(:message)
exception = message exception = message

47
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 TERMINAL_PREFIX = ENV['TERMINAL_PREFIX'].to_i || 0
CONTENT_PREFIX = ' '.freeze CONTENT_PREFIX = ' '.freeze
RENDERED_VIEW_DURATION = 100
ANSI_DEBUG = "\e[90m".freeze ANSI_DEBUG = "\e[90m".freeze
ANSI_INFO = SemanticLogger::AnsiColors::GREEN ANSI_INFO = SemanticLogger::AnsiColors::GREEN
ANSI_WARN = SemanticLogger::AnsiColors::YELLOW ANSI_WARN = SemanticLogger::AnsiColors::YELLOW
@ -46,7 +48,6 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
error: ANSI_ERROR, error: ANSI_ERROR,
fatal: ANSI_ERROR fatal: ANSI_ERROR
)) ))
@caller = nil
end end
def message def message
@ -77,14 +78,6 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
end end
end end
def draw_rails(char)
"#{color}#{char}#{color_map.clear}"
end
def continuation
draw_rails('┆')
end
def name def name
"#{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
@ -109,10 +102,17 @@ class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metri
private private
def draw_rails(char)
"#{color}#{char}#{color_map.clear}"
end
def continuation
draw_rails('┆')
end
def transform_log(log) def transform_log(log)
if log.name == 'ActionView::Base' 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 elsif log.name == 'Rails' && log.message
message = log.message.rstrip message = log.message.rstrip
message += "\n" if message.starts_with?('Completed 2') || message.starts_with?('Completed 3') 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 log
end 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) def wrap_message(message)
message, space_prefix = split_spaces_in_front(message) message, space_prefix = split_spaces_in_front(message)
message = Wrapper.wrap("#{CONTENT_COLOR_MAP[log.level]}#{message}", message = Wrapper.wrap("#{CONTENT_COLOR_MAP[log.level]}#{message}",

Loading…
Cancel
Save