|
|
@ -1,6 +1,7 @@ |
|
|
|
# Opinioned Rails custom formatter |
|
|
|
class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
NAME_MAX_SIZE = 25 |
|
|
|
CONTENT_PREFIX = ' '.freeze |
|
|
|
|
|
|
|
ANSI_DEBUG = "\e[90m".freeze |
|
|
|
ANSI_INFO = SemanticLogger::AnsiColors::GREEN |
|
|
@ -33,7 +34,12 @@ class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
def message |
|
|
|
return unless log.message |
|
|
|
|
|
|
|
" #{CONTENT_COLOR_MAP[log.level]}#{log.message}#{color_map.clear}" |
|
|
|
message = log.message |
|
|
|
if log.name == 'Rails' && message.starts_with?('Completed') |
|
|
|
message.rstrip! |
|
|
|
message += "\n" unless message.starts_with?('Completed 5') |
|
|
|
end |
|
|
|
"#{CONTENT_PREFIX}#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}" |
|
|
|
end |
|
|
|
|
|
|
|
def level |
|
|
@ -45,16 +51,14 @@ class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
"#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}" |
|
|
|
end |
|
|
|
|
|
|
|
def exception |
|
|
|
def exception # rubocop:disable Metrics/AbcSize |
|
|
|
return unless log.exception |
|
|
|
|
|
|
|
root_path = Rails.root.to_s |
|
|
|
backtrace = log.exception.backtrace.select { |line| line.starts_with?(root_path) } |
|
|
|
if backtrace.count.positive? |
|
|
|
"-- #{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear} #{color}#{log.exception.message}\n#{color} ↳ #{backtrace.join("\n ↳ ")}#{color_map.clear}\n\n" |
|
|
|
else |
|
|
|
"-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n" |
|
|
|
end |
|
|
|
stack = log.exception.backtrace.select { |line| line.starts_with?(root_path) } |
|
|
|
stack = stack.map { |line| line.delete_prefix("#{root_path}/") } |
|
|
|
|
|
|
|
"#{CONTENT_PREFIX}#{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear}: #{color}#{log.exception.message}#{color_map.clear}#{backtrace(stack)}" # rubocop:disable Layout/LineLength |
|
|
|
end |
|
|
|
|
|
|
|
def call(log, logger) |
|
|
@ -64,4 +68,13 @@ class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
|
|
|
|
[name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ') |
|
|
|
end |
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
def backtrace(stack) |
|
|
|
nil unless stack.count.positive? |
|
|
|
|
|
|
|
prefix = [name, level, tags, named_tags, duration, message, payload, CONTENT_PREFIX].compact.join(' ') |
|
|
|
"\n#{prefix}#{ANSI_ERROR}↳ #{stack.join("\n#{prefix}#{ANSI_ERROR}↳ ")}#{color_map.clear}" |
|
|
|
end |
|
|
|
end |