# 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_REVERSED_FATAL = "\e[1;30;41m".freeze 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 def initialize super(time_format: nil, color_map: ColorMap.new( debug: ANSI_DEBUG, info: ANSI_INFO, warn: ANSI_WARN, fatal: ANSI_ERROR )) end def message return unless log.message msg = log.message prefix = "#{color} #{color_map.clear}" case log.level when :info "#{prefix}#{SemanticLogger::AnsiColors::WHITE}#{msg}#{color_map.clear}" when :warn "#{prefix}#{ANSI_REVERSED_WARNING}#{msg}#{color_map.clear}" when :error "#{prefix}#{ANSI_REVERSED_ERROR}#{msg}#{color_map.clear}" when :fatal "#{prefix}#{ANSI_REVERSED_FATAL}#{msg}#{color_map.clear}" else "#{prefix}#{color}#{msg}#{color_map.clear}" end end def level "#{color}╣#{log.level.to_s.chr.upcase}╠#{color_map.clear}" end def name # "#{color}#{log.name.ljust(20)}#{color_map.clear}" "#{ANSI_DEBUG}#{log.name.rjust(20)}#{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