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.

69 lines
2.0 KiB

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