pvincent
11 months ago
6 changed files with 78 additions and 30 deletions
-
2Gemfile
-
7Gemfile.lock
-
1app/controllers/scores_controller.rb
-
13config/environments/development.rb
-
25lib/formatter/colored_formatter.rb
-
60lib/formatters/basic_formatter.rb
@ -1,25 +0,0 @@ |
|||||
require 'rainbow' |
|
||||
|
|
||||
# ColoredFormatter outputs colored message according to severity |
|
||||
class ColoredFormatter < Logger::Formatter |
|
||||
def call(severity, _time, _program_name, message) # rubocop:disable Metrics/MethodLength |
|
||||
# msg = message.is_a?(String) ? message : message.inspect |
|
||||
msg = message |
|
||||
tint = case severity |
|
||||
when 'DEBUG' |
|
||||
:darkgray |
|
||||
when 'INFO' |
|
||||
:white |
|
||||
when 'WARN' |
|
||||
# :orange |
|
||||
'#FFC482' |
|
||||
when 'ERROR' |
|
||||
:red |
|
||||
when 'FATAL' |
|
||||
:magenta |
|
||||
else |
|
||||
raise "unknown severity #{severity}" |
|
||||
end |
|
||||
"#{Rainbow('╞' + severity.ljust(5) + '╡').bg(:cyan).color(:darkgray)}╠ #{Rainbow(msg).color(tint)}\n" |
|
||||
end |
|
||||
end |
|
@ -0,0 +1,60 @@ |
|||||
|
# My Custom colorized formatter |
||||
|
class BasicFormatter < SemanticLogger::Formatters::Color |
||||
|
ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze |
||||
|
ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze |
||||
|
ANSI_GRAY = "\e[90m".freeze |
||||
|
|
||||
|
def initialize |
||||
|
super(time_format: nil, |
||||
|
color_map: ColorMap.new( |
||||
|
debug: ANSI_GRAY, |
||||
|
info: SemanticLogger::AnsiColors::GREEN, |
||||
|
warn: SemanticLogger::AnsiColors::YELLOW |
||||
|
)) |
||||
|
end |
||||
|
|
||||
|
def message |
||||
|
return unless log.message |
||||
|
|
||||
|
prefix = "#{color}--#{color_map.clear}" |
||||
|
|
||||
|
case log.level |
||||
|
when :info |
||||
|
"#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{log.message}#{color_map.clear}" |
||||
|
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 |
||||
|
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}#{color_map.clear}#{SemanticLogger::AnsiColors::WHITE}\n\t#{backtrace.join("\n\t")}#{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 |
||||
|
|
||||
|
[level, tags, named_tags, duration, name, message, payload, exception].compact.join(' ') |
||||
|
end |
||||
|
end |
Write
Preview
Loading…
Cancel
Save
Reference in new issue