|
|
@ -4,7 +4,7 @@ require 'io/console' |
|
|
|
require 'amazing_print' |
|
|
|
|
|
|
|
# Opinioned Rails custom formatter |
|
|
|
class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
class BasicFormatter < SemanticLogger::Formatters::Color # rubocop:disable Metrics/ClassLength |
|
|
|
NAME_MAX_SIZE = 25 |
|
|
|
TERMINAL_PREFIX = ENV['TERMINAL_PREFIX'].to_i || 0 |
|
|
|
CONTENT_PREFIX = ' '.freeze |
|
|
@ -51,20 +51,9 @@ class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
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}" |
|
|
|
message = transform_message(log) |
|
|
|
message = wrap_message(message) |
|
|
|
"#{CONTENT_COLOR_MAP[log.level]}#{message}#{color_map.clear}" |
|
|
|
end |
|
|
|
|
|
|
|
def payload |
|
|
@ -74,7 +63,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
first_line = lines.shift |
|
|
|
space_prefix = first_line.match(/^\s*/) |
|
|
|
lines = lines.map do |l| |
|
|
|
"#{before_message(true)}#{space_prefix}#{l}" |
|
|
|
"#{before_message(wrapped: true)}#{space_prefix}#{l}" |
|
|
|
end |
|
|
|
result = lines.unshift(first_line).join("\n") |
|
|
|
result.sub!(/\s*/) { |m| '-' * m.length } |
|
|
@ -111,11 +100,6 @@ class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
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 |
|
|
@ -125,13 +109,34 @@ class BasicFormatter < SemanticLogger::Formatters::Color |
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
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 transform_message(log) |
|
|
|
return log.message unless log.name == 'Rails' && log.message.starts_with?('Completed') |
|
|
|
|
|
|
|
message = log.message.rstrip! |
|
|
|
message += "\n" unless message.starts_with?('Completed 5') |
|
|
|
message |
|
|
|
end |
|
|
|
|
|
|
|
def compute_useful_length |
|
|
|
wrap_length = IO.console.winsize[1] - TERMINAL_PREFIX - before_message.length + CONTENT_PREFIX.length + 12 |
|
|
|
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! |
|
|
|
100 # FIXME: CONSTANTIZE, only useful in DEBUGGER, no IO.console detected! |
|
|
|
end |
|
|
|
|
|
|
|
def before_message(wrapped = false) |
|
|
|
def before_message(wrapped: false) |
|
|
|
[name, wrapped ? continuation : level, tags, named_tags, duration].compact.join(' ') + CONTENT_PREFIX |
|
|
|
end |
|
|
|
|
|
|
|