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.

58 lines
1.6 KiB

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