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
1.9 KiB

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