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.

67 lines
1.9 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
  1. # Opinioned Rails custom formatter
  2. class BasicFormatter < SemanticLogger::Formatters::Color
  3. NAME_MAX_SIZE = 25
  4. ANSI_DEBUG = "\e[90m".freeze
  5. ANSI_INFO = SemanticLogger::AnsiColors::GREEN
  6. ANSI_WARN = SemanticLogger::AnsiColors::YELLOW
  7. ANSI_ERROR = SemanticLogger::AnsiColors::RED
  8. ANSI_FATAL = SemanticLogger::AnsiColors::MAGENTA
  9. ANSI_NEUTRAL_INFO = SemanticLogger::AnsiColors::WHITE
  10. ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze
  11. ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze
  12. ANSI_REVERSED_FATAL = "\e[1;30;41m".freeze
  13. CONTENT_COLOR_MAP = ColorMap.new(
  14. debug: ANSI_DEBUG,
  15. info: ANSI_NEUTRAL_INFO,
  16. warn: ANSI_REVERSED_WARNING,
  17. error: ANSI_REVERSED_ERROR,
  18. fatal: ANSI_REVERSED_FATAL
  19. )
  20. def initialize
  21. super(color_map: ColorMap.new(
  22. debug: ANSI_DEBUG,
  23. info: ANSI_INFO,
  24. warn: ANSI_WARN,
  25. error: ANSI_ERROR,
  26. fatal: ANSI_ERROR
  27. ))
  28. end
  29. def message
  30. return unless log.message
  31. " #{CONTENT_COLOR_MAP[log.level]}#{log.message}#{color_map.clear}"
  32. end
  33. def level
  34. level = log.level == :info ? ' ' : log.level.to_s.chr.upcase
  35. "#{color}#{level}#{color_map.clear}"
  36. end
  37. def name
  38. "#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}"
  39. end
  40. def exception
  41. return unless log.exception
  42. root_path = Rails.root.to_s
  43. backtrace = log.exception.backtrace.select { |line| line.starts_with?(root_path) }
  44. if backtrace.count.positive?
  45. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear} #{color}#{log.exception.message}\n#{color}#{backtrace.join("\n")}#{color_map.clear}\n\n"
  46. else
  47. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n"
  48. end
  49. end
  50. def call(log, logger)
  51. self.color = color_map[log.level]
  52. self.log = log
  53. self.logger = logger
  54. [name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ')
  55. end
  56. end