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.

53 lines
1.2 KiB

6 months ago
6 months ago
6 months ago
  1. require_relative 'ansi_wrapper'
  2. require_relative 'ansi_colors'
  3. require 'io/console'
  4. require 'amazing_print'
  5. require 'json'
  6. class AnsiFormatter < SemanticLogger::Formatters::Color
  7. include AnsiColors
  8. ANSI_DEBUG = TEXT_GRAY_400
  9. ANSI_INFO = TEXT_GRAY_100
  10. ANSI_WARN = BG_YELLOW + TEXT_BLACK
  11. ANSI_ERROR = BG_RED + TEXT_BLACK
  12. ANSI_FATAL = DARK_BG_RED + TEXT_BLACK
  13. def initialize
  14. super(color_map: ColorMap.new(
  15. debug: ANSI_DEBUG,
  16. info: ANSI_INFO,
  17. warn: ANSI_WARN,
  18. error: ANSI_ERROR,
  19. fatal: ANSI_FATAL
  20. ))
  21. end
  22. def message
  23. return unless log.message
  24. colorize(log.message, color_map[log.level])
  25. end
  26. def call(log, logger)
  27. self.log = log
  28. self.logger = logger
  29. self.color = color_map[log.level]
  30. wrap_level(level, message, payload, exception)
  31. end
  32. private
  33. def colorize(text, color)
  34. "#{color}#{text}#{color_map.clear}"
  35. end
  36. def wrap_level(level, *items)
  37. prefix = " #{colorize('>', color)} "
  38. continuation = " #{colorize('#', color)} "
  39. items.map do |item|
  40. AnsiWrapper.wrap(item, 100, prefix, continuation)
  41. end.compact.join("\n") # FIXME: previously was: join(' ')
  42. end
  43. end