You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
65 lines
2.1 KiB
65 lines
2.1 KiB
# My Custom colorized formatter
|
|
class BasicFormatter < SemanticLogger::Formatters::Color
|
|
NAME_MAX_SIZE = 25
|
|
|
|
ANSI_DEBUG = "\e[90m".freeze
|
|
ANSI_INFO = SemanticLogger::AnsiColors::GREEN
|
|
ANSI_WARN = SemanticLogger::AnsiColors::YELLOW
|
|
ANSI_ERROR = SemanticLogger::AnsiColors::RED
|
|
ANSI_FATAL = SemanticLogger::AnsiColors::MAGENTA
|
|
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
|
|
|
|
def initialize
|
|
super(time_format: nil,
|
|
color_map: ColorMap.new(debug: ANSI_DEBUG, info: ANSI_INFO, warn: ANSI_WARN, error: ANSI_ERROR,
|
|
fatal: ANSI_ERROR))
|
|
@content_color_map = ColorMap.new(debug: ANSI_DEBUG, info: ANSI_NEUTRAL_INFO, warn: ANSI_REVERSED_WARNING,
|
|
error: ANSI_REVERSED_ERROR, fatal: ANSI_REVERSED_FATAL)
|
|
end
|
|
|
|
def message
|
|
return unless log.message
|
|
|
|
" #{@content_color_map[log.level]}#{log.message}#{color_map.clear}"
|
|
end
|
|
|
|
def level
|
|
level = log.level == :info ? ' ' : log.level.to_s.chr.upcase
|
|
"#{color}╣#{level}╠#{color_map.clear}"
|
|
end
|
|
|
|
def name
|
|
"#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}"
|
|
end
|
|
|
|
def process_info
|
|
fname = file_name_and_line
|
|
"#{color}[#{fname}]#{color_map.clear}" if fname
|
|
end
|
|
|
|
def exception
|
|
return unless log.exception
|
|
|
|
root_path = Rails.root.to_s
|
|
backtrace = log.exception.backtrace.select do |line|
|
|
line.starts_with?(root_path)
|
|
end
|
|
|
|
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
|
|
end
|
|
|
|
def call(log, logger)
|
|
self.color = color_map[log.level]
|
|
self.log = log
|
|
self.logger = logger
|
|
|
|
[name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ')
|
|
end
|
|
end
|