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.

60 lines
1.8 KiB

  1. # My Custom colorized formatter
  2. class BasicFormatter < SemanticLogger::Formatters::Color
  3. ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze
  4. ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze
  5. ANSI_GRAY = "\e[90m".freeze
  6. def initialize
  7. super(time_format: nil,
  8. color_map: ColorMap.new(
  9. debug: ANSI_GRAY,
  10. info: SemanticLogger::AnsiColors::GREEN,
  11. warn: SemanticLogger::AnsiColors::YELLOW
  12. ))
  13. end
  14. def message
  15. return unless log.message
  16. prefix = "#{color}--#{color_map.clear}"
  17. case log.level
  18. when :info
  19. "#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{log.message}#{color_map.clear}"
  20. when :warn
  21. "#{prefix} #{ANSI_REVERSED_WARNING}#{log.message}#{color_map.clear}"
  22. when :error, :fatal
  23. "#{prefix} #{ANSI_REVERSED_ERROR}#{log.message}#{color_map.clear}"
  24. else
  25. "#{prefix} #{color}#{log.message}#{color_map.clear}"
  26. end
  27. end
  28. def process_info
  29. fname = file_name_and_line
  30. "#{color}[#{fname}]#{color_map.clear}" if fname
  31. end
  32. def exception
  33. return unless log.exception
  34. root_path = Rails.root.to_s
  35. backtrace = log.exception.backtrace.select do |line|
  36. line.starts_with?(root_path)
  37. end
  38. if backtrace.count.positive?
  39. "-- #{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"
  40. else
  41. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n"
  42. end
  43. end
  44. def call(log, logger)
  45. self.color = color_map[log.level]
  46. self.log = log
  47. self.logger = logger
  48. [level, tags, named_tags, duration, name, message, payload, exception].compact.join(' ')
  49. end
  50. end