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.

51 lines
1.4 KiB

2 months ago
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. 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(time_format: nil)
  10. super
  11. end
  12. def call(log, logger)
  13. self.color = color_map[log.level]
  14. self.log = log
  15. self.logger = logger
  16. wrap_level(MAX_LENGTH_LINE, duration, named_tags, message, payload, exception)
  17. rescue StandardError => e
  18. puts "Error during formatting: #{e.message}"
  19. puts e.backtrace.join("\n")
  20. end
  21. private
  22. def wrap_level(length, *items)
  23. prime = "#{build_prime} "
  24. continuation = "#{build_continuation} "
  25. items
  26. .map { |item| Semantic::AnsiWrapper.wrap(item, length, prime, continuation) }
  27. .compact.join("\n")
  28. end
  29. def message
  30. return unless log.message
  31. log.level == :info ? colorize(log.message, TEXT_WHITE) : colorize(log.message)
  32. end
  33. def process_info
  34. fname = file_name_and_line
  35. colorize fname if fname
  36. end
  37. def time = @time_format ? colorize(format_time(log.time), color) : nil
  38. def build_prefix(char) = [time, tags, origin, colorize(char)].compact.join ' '
  39. def build_prime = build_prefix(PRIME)
  40. def build_continuation = build_prefix(CONTINATION)
  41. end
  42. end