|
@ -3,73 +3,56 @@ module Semantic |
|
|
class BasicFormatter < AbstractFormatter |
|
|
class BasicFormatter < AbstractFormatter |
|
|
ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze |
|
|
ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze |
|
|
ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze |
|
|
ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze |
|
|
ANSI_GRAY = "\e[90m".freeze |
|
|
|
|
|
|
|
|
MAX_LENGTH_LINE = 255 |
|
|
|
|
|
PRIME = '▍'.freeze |
|
|
|
|
|
CONTINATION = '▕'.freeze |
|
|
|
|
|
|
|
|
def initialize |
|
|
def initialize |
|
|
super(time_format: '%H:%M:%S', |
|
|
|
|
|
color_map: ColorMap.new( |
|
|
|
|
|
debug: ANSI_GRAY, |
|
|
|
|
|
info: SemanticLogger::AnsiColors::GREEN, |
|
|
|
|
|
warn: SemanticLogger::AnsiColors::YELLOW |
|
|
|
|
|
|
|
|
super(color_map: ColorMap.new( |
|
|
|
|
|
debug: TEXT_GRAY_400, |
|
|
|
|
|
info: TEXT_GREEN, |
|
|
|
|
|
warn: TEXT_YELLOW, |
|
|
|
|
|
error: TEXT_COLOR_ORANGE, |
|
|
|
|
|
fatal: TEXT_RED |
|
|
)) |
|
|
)) |
|
|
@time_format = nil if File.exist?(File.join(Rails.root, 'Procfile.dev')) |
|
|
|
|
|
|
|
|
@time_format = File.exist?(File.join(Rails.root, 'Procfile.dev')) ? nil : '%H:%M:%S' |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def exception |
|
|
|
|
|
super_exception = super |
|
|
|
|
|
"EXCEPTION -- #{super_exception}" if super_exception |
|
|
|
|
|
|
|
|
def call(log, logger) |
|
|
|
|
|
self.color = color_map[log.level] |
|
|
|
|
|
self.log = log |
|
|
|
|
|
self.logger = logger |
|
|
|
|
|
|
|
|
|
|
|
wrap_level(MAX_LENGTH_LINE, duration, named_tags, message, payload, exception) |
|
|
|
|
|
rescue StandardError => e |
|
|
|
|
|
puts "Error during formatting: #{e.message}" |
|
|
|
|
|
puts e.backtrace.join("\n") |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def time |
|
|
|
|
|
"#{color}#{format_time(log.time)}#{color_map.clear}" if time_format |
|
|
|
|
|
|
|
|
private |
|
|
|
|
|
|
|
|
|
|
|
def wrap_level(length, *items) |
|
|
|
|
|
prime = "#{build_prime} " |
|
|
|
|
|
continuation = "#{build_continuation} " |
|
|
|
|
|
items |
|
|
|
|
|
.map { |item| Semantic::AnsiWrapper.wrap(item, length, prime, continuation) } |
|
|
|
|
|
.compact.join("\n") |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def message |
|
|
def message |
|
|
return unless log.message |
|
|
return unless log.message |
|
|
|
|
|
|
|
|
prefix = "#{color}--#{color_map.clear}" |
|
|
|
|
|
|
|
|
|
|
|
case log.level |
|
|
|
|
|
when :info |
|
|
|
|
|
message = log.message |
|
|
|
|
|
message = message&.rstrip if log.name == 'Rails' && message.starts_with?('Completed') |
|
|
|
|
|
if log.name == 'Rails' && message.starts_with?('Started') |
|
|
|
|
|
message = message.split('for')[0] |
|
|
|
|
|
puts '' if Rails.env.development? |
|
|
|
|
|
end |
|
|
|
|
|
if log.name == 'Rails' || log.name == 'ActionView::Base' |
|
|
|
|
|
"#{prefix} #{ANSI_GRAY}#{message}#{color_map.clear}" |
|
|
|
|
|
else |
|
|
|
|
|
"#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{message}#{color_map.clear}" |
|
|
|
|
|
end |
|
|
|
|
|
when :warn |
|
|
|
|
|
"#{prefix} #{ANSI_REVERSED_WARNING}#{log.message}#{color_map.clear}" |
|
|
|
|
|
when :error, :fatal |
|
|
|
|
|
"#{prefix} #{ANSI_REVERSED_ERROR}#{log.message}#{color_map.clear}" |
|
|
|
|
|
else |
|
|
|
|
|
"#{prefix} #{color}#{log.message}#{color_map.clear}" |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
log.level == :info ? colorize(log.message, TEXT_WHITE) : colorize(log.message) |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def process_info |
|
|
def process_info |
|
|
fname = file_name_and_line |
|
|
fname = file_name_and_line |
|
|
"#{color}[#{fname}]#{color_map.clear}" if fname |
|
|
|
|
|
|
|
|
colorize fname if fname |
|
|
end |
|
|
end |
|
|
|
|
|
|
|
|
def call(log, logger) |
|
|
|
|
|
self.color = color_map[log.level] |
|
|
|
|
|
self.log = log |
|
|
|
|
|
self.logger = logger |
|
|
|
|
|
|
|
|
|
|
|
if @time_format |
|
|
|
|
|
[time, level, process_info, tags, named_tags, duration, name, message, payload, |
|
|
|
|
|
exception].compact.join(' ') |
|
|
|
|
|
else |
|
|
|
|
|
[tags, named_tags, duration, name, message, payload, exception].compact.join(' ') |
|
|
|
|
|
end |
|
|
|
|
|
rescue StandardError => e |
|
|
|
|
|
puts "Error during formatting: #{e.message}" |
|
|
|
|
|
puts e.backtrace.join("\n") |
|
|
|
|
|
end |
|
|
|
|
|
|
|
|
def time = @time_format ? colorize(format_time(log.time), color) : nil |
|
|
|
|
|
def build_prefix(char) = [time, tags, origin, colorize(char)].compact.join ' ' |
|
|
|
|
|
def build_prime = build_prefix(PRIME) |
|
|
|
|
|
def build_continuation = build_prefix(CONTINATION) |
|
|
end |
|
|
end |
|
|
end |
|
|
end |