Browse Source

rails message logging

pagy
pvincent 11 months ago
parent
commit
9af901f93e
  1. 3
      config/environments/development.rb
  2. 31
      lib/formatter/colored_formatter.rb

3
config/environments/development.rb

@ -1,6 +1,5 @@
require 'active_support/core_ext/integer/time'
require_relative '../../lib/formatter/colored_formatter'
Rainbow.enabled = true
Rails.application.configure do # rubocop:disable Metrics/BlockLength
# Settings specified here will take precedence over those in config/application.rb.
@ -79,3 +78,5 @@ Rails.application.configure do # rubocop:disable Metrics/BlockLength
# Colorful Simple Logger Formatter
config.log_formatter = ColoredFormatter.new
end
Rainbow.enabled = true

31
lib/formatter/colored_formatter.rb

@ -2,19 +2,22 @@ require 'rainbow'
# ColoredFormatter outputs colored message according to severity
class ColoredFormatter < Logger::Formatter
def call(severity, _time, _program_name, message) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
full_message = message.is_a?(String) ? message : message.inspect
case severity
when 'DEBUG'
"#{Rainbow(severity).gray.bright.underline.bg(:yellow)} - #{Rainbow(full_message).gray}\n"
when 'INFO'
"#{Rainbow(severity).green.bright.underline.bg(:yellow)} - #{Rainbow(full_message).green}\n"
when 'WARN'
"#{Rainbow(severity).orange.bright.underline} - #{Rainbow(full_message).orange}\n"
when 'ERROR'
"#{Rainbow(severity).red.bright.underline} - #{Rainbow(full_message).red}\n"
else
"unknown severity #{severity}\n"
end
def call(severity, _time, _program_name, message) # rubocop:disable Metrics/MethodLength
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(message).color(tint)}\n"
end
end
Loading…
Cancel
Save