|
|
require_relative 'wrapper' require_relative 'base' require 'io/console' require 'amazing_print'
# Opinioned Rails custom formatter class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metrics/ClassLength NAME_MAX_SIZE = 25
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 ANSI_ERROR = "\e[91m".freeze ANSI_NEUTRAL_INFO = SemanticLogger::AnsiColors::WHITE ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze 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 )
EXCLUDE_LAMBDA = lambda { |log| if log.name == 'ActionView::Base' !log.message.starts_with?(' Rendering') elsif log.name == 'Rails' && !log.message.nil? log.message.exclude?('Started GET "/rails/live/reload') elsif log.name == 'ActiveRecord::Base' log.message.exclude?('↳ lib/formatters/basic_formatter.rb') else true end }
def initialize 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
message = wrap_message(log.message) "#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}" end
def payload return unless log.payload
lines = log.payload.ai(ruby19_syntax: true, indent: 2).split("\n") first_line = lines.shift space_prefix = first_line.match(/^\s*/) lines = lines.map do |l| "#{before_message(wrapped: true)}#{space_prefix}#{l}" end result = lines.unshift(first_line).join("\n") result.sub!(/\s*/) { |m| '-' * m.length } result end
def level case log.level when :info, :debug then draw_rails ' ' else draw_rails(log.level.to_s.chr.upcase) end end
def name "#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}" end
def exception # rubocop:disable Metrics/AbcSize return unless log.exception
root_path = Rails.root.to_s 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 end
def call(log, logger) self.log = transform_log(log) self.color = color_map[self.log.level] self.logger = logger
before_message + [message, payload, exception].compact.join(' ') end
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 = 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') log.message = message end 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}", before_message(wrapped: true) + space_prefix.to_s, compute_useful_length - space_prefix.length) "#{space_prefix}#{message}" end
def split_spaces_in_front(message) md = message.match(/^\s*/) [md.post_match, md.match(0)] end
def compute_useful_length IO.console.winsize[1] - TERMINAL_PREFIX - before_message.length + CONTENT_PREFIX.length + 12
rescue StandardError 100 # FIXME: CONSTANTIZE, only useful in DEBUGGER, no IO.console detected! end
def before_message(wrapped: false) [name, wrapped ? continuation : level, tags, named_tags, duration].compact.join(' ') + CONTENT_PREFIX end
def backtrace(stack) return unless stack.count.positive?
"\n#{before_message} #{ANSI_ERROR}↳ #{stack.join("\n#{before_message} #{ANSI_ERROR}↳ ")}#{color_map.clear}" end
def color_content color end end
|