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.

93 lines
2.5 KiB

6 months ago
6 months ago
5 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
5 months ago
6 months ago
5 months ago
5 months ago
6 months ago
6 months ago
6 months ago
5 months ago
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. # wraps meanwhile takes care of ansi colors
  7. class AnsiFormatter < SemanticLogger::Formatters::Color
  8. include AnsiColors
  9. CENTER_SIZE = 20
  10. FOREMAN_PREFIX_LENGTH = 18
  11. FAILOVER_WRAP = 80
  12. def initialize
  13. super(color_map:
  14. ColorMap.new(
  15. debug: CLEAR + TEXT_GRAY_400,
  16. info: CLEAR + TEXT_GRAY_100,
  17. warn: CLEAR + BG_YELLOW + TEXT_BLACK,
  18. error: CLEAR + BG_RED + TEXT_WHITE,
  19. fatal: CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE
  20. ))
  21. end
  22. def call(log, logger)
  23. log = AnsiFilter.alter(log)
  24. self.log = log
  25. self.logger = logger
  26. self.color = color_map[log.level]
  27. wrap_level(compute_useful_length, message, payload, exception)
  28. end
  29. private
  30. def origin = colorize(centerize(log.name), TEXT_CYAN)
  31. def build_prefix(char) = "#{origin}#{colorize(char)}"
  32. def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
  33. def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
  34. def stackisize(items) = items.length.positive? ? "\n#{items.join("\n")}" : ''
  35. def build_dimensions(dimensions)
  36. "#{origin} #{dimensions.rails} "
  37. end
  38. def compute_useful_length
  39. IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
  40. rescue StandardError
  41. FAILOVER_WRAP
  42. end
  43. def message
  44. colorize(log.message) if log.message
  45. end
  46. def exception
  47. return unless log.exception
  48. exc = log.exception
  49. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  50. message = colorize(exc.message.chomp(''), color_map[:error])
  51. backtrace = stackisize(clean_backtrace(exc.backtrace))
  52. "#{clazz}#{message}#{backtrace}"
  53. end
  54. def clean_backtrace(backtrace)
  55. root_path = Rails.root.to_s
  56. backtrace = backtrace.select { |line| line.starts_with?(root_path) }
  57. backtrace.map { |line| line.delete_prefix("#{root_path}/") }
  58. end
  59. def level_char
  60. case log.level
  61. when :info, :debug then ' '
  62. else log.level.to_s.chr.upcase
  63. end
  64. end
  65. def wrap_level(length, *items)
  66. prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
  67. continuation = build_prefix('┆')
  68. log.dimensions&.before&.times { items.unshift('') }
  69. log.dimensions&.after&.times { items << '' }
  70. items.map do |item|
  71. AnsiWrapper.wrap(item, length, prefix, continuation)
  72. end.compact.join("\n")
  73. end
  74. end