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

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. # My Custom colorized 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. def initialize
  14. super(time_format: nil,
  15. color_map: ColorMap.new(debug: ANSI_DEBUG, info: ANSI_INFO, warn: ANSI_WARN, error: ANSI_ERROR,
  16. fatal: ANSI_ERROR))
  17. @content_color_map = ColorMap.new(debug: ANSI_DEBUG, info: ANSI_NEUTRAL_INFO, warn: ANSI_REVERSED_WARNING,
  18. error: ANSI_REVERSED_ERROR, fatal: ANSI_REVERSED_FATAL)
  19. end
  20. def message
  21. return unless log.message
  22. " #{@content_color_map[log.level]}#{log.message}#{color_map.clear}"
  23. end
  24. def level
  25. level = log.level == :info ? ' ' : log.level.to_s.chr.upcase
  26. "#{color}#{level}#{color_map.clear}"
  27. end
  28. def name
  29. "#{ANSI_DEBUG}#{log.name.truncate(NAME_MAX_SIZE).center(NAME_MAX_SIZE)}#{color_map.clear}"
  30. end
  31. def process_info
  32. fname = file_name_and_line
  33. "#{color}[#{fname}]#{color_map.clear}" if fname
  34. end
  35. def exception
  36. return unless log.exception
  37. root_path = Rails.root.to_s
  38. backtrace = log.exception.backtrace.select do |line|
  39. line.starts_with?(root_path)
  40. end
  41. if backtrace.count.positive?
  42. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear} #{color}#{log.exception.message}\n#{color}#{backtrace.join("\n")}#{color_map.clear}\n\n"
  43. else
  44. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n"
  45. end
  46. end
  47. def call(log, logger)
  48. self.color = color_map[log.level]
  49. self.log = log
  50. self.logger = logger
  51. [name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ')
  52. end
  53. end