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.

55 lines
1.5 KiB

2 months ago
2 months ago
3 weeks ago
2 months ago
2 months ago
2 months ago
2 months ago
2 months ago
  1. module Semantic
  2. # My Custom colorized formatter
  3. class BasicFormatter < AbstractFormatter
  4. MAX_LENGTH_LINE = 255
  5. PRIME = '▍'.freeze
  6. CONTINATION = '▕'.freeze
  7. def initialize(time_format: nil)
  8. super(time_format:, color_map: ColorMap.new(
  9. debug: TEXT_GRAY_400,
  10. info: TEXT_GREEN,
  11. warn: TEXT_YELLOW,
  12. error: LIGHT_TEXT_RED,
  13. fatal: TEXT_MAGENTA
  14. ))
  15. end
  16. def call(log, logger)
  17. self.color = color_map[log.level]
  18. self.log = log
  19. self.logger = logger
  20. wrap_level(MAX_LENGTH_LINE, duration, named_tags, message, payload, exception)
  21. rescue StandardError => e
  22. puts "Error during formatting: #{e.message}"
  23. puts e.backtrace.join("\n")
  24. end
  25. private
  26. def wrap_level(length, *items)
  27. prime = "#{build_prime} "
  28. continuation = "#{build_continuation} "
  29. items
  30. .map { |item| Semantic::AnsiWrapper.wrap(item, length, prime, continuation) }
  31. .compact.join("\n")
  32. end
  33. def message
  34. return unless log.message
  35. log.level == :info ? colorize(log.message, TEXT_WHITE) : colorize(log.message)
  36. end
  37. def process_info
  38. fname = file_name_and_line
  39. colorize fname if fname
  40. end
  41. def time = @time_format ? colorize(format_time(log.time), color) : nil
  42. def build_prefix(char) = [time, tags, origin, colorize(char)].compact.join ' '
  43. def build_prime = build_prefix(PRIME)
  44. def build_continuation = build_prefix(CONTINATION)
  45. end
  46. end