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.

101 lines
2.7 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
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
5 months ago
5 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 build_terminus = "#{origin} #{AnsiFilter::TERMINUS_STRING} "
  33. def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
  34. def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
  35. def stackisize(items) = items.length.positive? ? "\n#{items.join("\n")}" : ''
  36. def build_dimensions(dimensions)
  37. "#{origin} #{dimensions.rails} "
  38. end
  39. def compute_useful_length
  40. IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
  41. rescue StandardError
  42. FAILOVER_WRAP
  43. end
  44. def message
  45. colorize(log.message) if log.message
  46. end
  47. def exception
  48. return unless log.exception
  49. exc = log.exception
  50. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  51. message = colorize(exc.message.chomp(''), color_map[:error])
  52. backtrace = stackisize(clean_backtrace(exc.backtrace))
  53. "#{clazz}#{message}#{backtrace}"
  54. end
  55. def clean_backtrace(backtrace)
  56. root_path = Rails.root.to_s
  57. backtrace = backtrace.select { |line| line.starts_with?(root_path) }
  58. backtrace.map { |line| line.delete_prefix("#{root_path}/") }
  59. end
  60. def level_char
  61. case log.level
  62. when :info, :debug then ' '
  63. else log.level.to_s.chr.upcase
  64. end
  65. end
  66. def wrap_level(length, *items)
  67. prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
  68. continuation = build_prefix('┆')
  69. result = items.map do |item|
  70. AnsiWrapper.wrap(item, length, prefix, continuation)
  71. end
  72. if log.dimensions&.terminus
  73. terminus = AnsiWrapper.wrap(' ', length, build_terminus)
  74. result << terminus
  75. end
  76. log.dimensions&.before&.times { result.unshift('') }
  77. log.dimensions&.after&.times { result << '' }
  78. result.compact.join("\n")
  79. end
  80. end