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.

149 lines
4.2 KiB

6 months ago
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
6 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
5 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_relative 'ansi_dimensions'
  4. require 'io/console'
  5. require 'amazing_print'
  6. require 'json'
  7. # wraps meanwhile takes care of ansi colors
  8. class AnsiFormatter < SemanticLogger::Formatters::Color
  9. include AnsiColors
  10. CENTER_SIZE = 20
  11. FOREMAN_PREFIX_LENGTH = 18
  12. FAILOVER_WRAP = 80
  13. CHAR_FATAL = '⯶'.freeze
  14. TERMINUS_STRING = '╙─╜'.freeze
  15. def initialize
  16. super(color_map:
  17. ColorMap.new(
  18. debug: CLEAR + TEXT_GRAY_400,
  19. info: CLEAR + TEXT_GRAY_100,
  20. warn: CLEAR + BG_YELLOW + TEXT_BLACK,
  21. error: CLEAR + BG_RED + TEXT_WHITE,
  22. fatal: CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE
  23. ))
  24. @memory = nil
  25. puts 'INITIALIZED2'
  26. end
  27. def call(log, logger)
  28. log = alter(log)
  29. self.log = log
  30. self.logger = logger
  31. self.color = color_map[log.level]
  32. wrap_level(compute_useful_length, message, payload, exception)
  33. end
  34. def reject(log)
  35. log.name == 'ScoresController' || false
  36. false
  37. end
  38. private
  39. def alter(log)
  40. if log.name == 'Rails'
  41. if log.message
  42. log.message.chomp!('')
  43. if log.message.starts_with?('Started')
  44. rails = '╓─╖'
  45. before = 1
  46. if @memory
  47. rails = "#{@memory}"
  48. before = 0
  49. end
  50. log.dimensions = AnsiDimensions.new(rails:, before:)
  51. @memory = nil
  52. elsif log.message.starts_with?('Completed 2')
  53. log.dimensions = AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
  54. elsif log.message.starts_with?('Completed 3')
  55. @memory = '║'
  56. log.dimensions = AnsiDimensions.new(rails: "#{@memory}")
  57. elsif log.message.starts_with?('Completed 4')
  58. log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
  59. elsif log.message.starts_with?('Completed 5')
  60. log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
  61. end
  62. elsif log.exception
  63. log.dimensions = AnsiDimensions.new(
  64. rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
  65. after: 1,
  66. terminus: true
  67. )
  68. end
  69. end
  70. log
  71. end
  72. def draw_fatal(char = CHAR_FATAL)
  73. AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
  74. end
  75. def origin = colorize(centerize(log.name), TEXT_CYAN)
  76. def build_prefix(char) = "#{origin}#{colorize(char)}"
  77. def build_terminus = "#{origin} #{TERMINUS_STRING} "
  78. def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
  79. def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
  80. def stackisize(items) = items.length.positive? ? "\n#{items.join("\n")}" : ''
  81. def build_dimensions(dimensions)
  82. "#{origin} #{dimensions.rails} "
  83. end
  84. def compute_useful_length
  85. IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
  86. rescue StandardError
  87. FAILOVER_WRAP
  88. end
  89. def message
  90. colorize(log.message) if log.message
  91. end
  92. def exception
  93. return unless log.exception
  94. exc = log.exception
  95. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  96. message = colorize(exc.message.chomp(''), color_map[:error])
  97. backtrace = stackisize(clean_backtrace(exc.backtrace))
  98. "#{clazz}#{message}#{backtrace}"
  99. end
  100. def clean_backtrace(backtrace)
  101. root_path = Rails.root.to_s
  102. backtrace = backtrace.select { |line| line.starts_with?(root_path) }
  103. backtrace.map { |line| line.delete_prefix("#{root_path}/") }
  104. end
  105. def level_char
  106. case log.level
  107. when :info, :debug then ' '
  108. else log.level.to_s.chr.upcase
  109. end
  110. end
  111. def wrap_level(length, *items)
  112. prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
  113. continuation = build_prefix('┆')
  114. result = items.map do |item|
  115. AnsiWrapper.wrap(item, length, prefix, continuation)
  116. end
  117. if log.dimensions&.terminus
  118. terminus = AnsiWrapper.wrap(' ', length, build_terminus)
  119. result << terminus
  120. end
  121. log.dimensions&.before&.times { result.unshift('') }
  122. log.dimensions&.after&.times { result << '' }
  123. result.compact.join("\n")
  124. end
  125. end