|
|
require_relative 'wrapper' require_relative 'base' require 'io/console' require 'amazing_print'
# Opinioned Rails custom formatter class BasicFormatter < SemanticLogger::Formatters::Color NAME_MAX_SIZE = 25
TERMINAL_PREFIX = ENV['TERMINAL_PREFIX'].to_i || 0
CONTENT_PREFIX = ' '.freeze
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 = log.message if log.name == 'Rails' && message.starts_with?('Completed') message.rstrip! message += "\n" unless message.starts_with?('Completed 5') end
wrap_length = compute_useful_length md = message.match(/^\s*/) space_prefix = md.match(0) message = md.post_match message = Wrapper.wrap("#{CONTENT_COLOR_MAP[log.level]}#{message}", before_message(true) + space_prefix.to_s, wrap_length - md.end(0))
"#{CONTENT_COLOR_MAP[log.level]}#{space_prefix}#{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(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 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
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) unless log.message.is_a?(String) log.payload = log.message log.message = nil end
self.color = color_map[log.level] self.log = log self.logger = logger
before_message + [message, payload, exception].compact.join(' ') end
private
def compute_useful_length wrap_length = IO.console.winsize[1] - TERMINAL_PREFIX - before_message.length + CONTENT_PREFIX.length + 12
rescue StandardError wrap_length = 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) nil 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
|