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.

194 lines
6.0 KiB

6 months ago
5 months ago
6 months ago
6 months ago
5 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
5 months ago
5 months ago
5 months ago
5 months ago
6 months ago
5 months ago
6 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_relative 'ansi_dimensions'
  4. require_relative 'ansi_common'
  5. require 'io/console'
  6. require 'amazing_print'
  7. require 'json'
  8. # wraps meanwhile takes care of ansi colors
  9. class AnsiFormatter < SemanticLogger::Formatters::Color
  10. include AnsiColors
  11. CENTER_SIZE = 20
  12. FOREMAN_PREFIX_LENGTH = 18
  13. FAILOVER_WRAP = 80
  14. CHAR_FATAL = '⯶'.freeze
  15. TERMINUS_STRING = '╙─╜'.freeze
  16. RENDERED_VIEW_DURATION = 100
  17. TOTAL_RENDERED_VIEW_DURATION = RENDERED_VIEW_DURATION * 5
  18. def initialize
  19. super(color_map:
  20. ColorMap.new(
  21. debug: CLEAR + TEXT_GRAY_400,
  22. info: CLEAR + TEXT_GRAY_100,
  23. warn: CLEAR + BG_YELLOW + TEXT_BLACK,
  24. error: CLEAR + BG_RED + TEXT_WHITE,
  25. fatal: CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE
  26. ))
  27. @memory = nil
  28. end
  29. def call(log, logger)
  30. log = alter(log)
  31. self.log = log
  32. self.logger = logger
  33. self.color = color_map[log.level]
  34. wrap_level(compute_useful_length, message, payload, exception)
  35. end
  36. def reject(log)
  37. return true if log.name == 'ActionView::Base' && log.message&.starts_with?(' Rendering')
  38. true if log.name == 'Rails' && log.message&.starts_with?('Loaded')
  39. end
  40. private
  41. def two_captures_last_as_bold(message, regex)
  42. match = message.match(regex)
  43. return "unmatched: #{message}" unless match
  44. m1, m2 = match.captures
  45. "#{m1}#{BOLD}#{m2}#{CLEAR}"
  46. end
  47. def alter(log)
  48. if log.name == 'Rails'
  49. if log.message
  50. log.message.lstrip!
  51. log.message.chomp!('')
  52. if log.message.starts_with?('Started')
  53. rails = '╓─╖'
  54. before = 1
  55. if @memory
  56. rails = "#{@memory}"
  57. before = 0
  58. end
  59. log.dimensions = AnsiDimensions.new(rails:, before:)
  60. @memory = nil
  61. log.message = two_captures_last_as_bold(log.message, /(^Started \w* )"(.*?)"/)
  62. elsif log.message.starts_with?('Completed')
  63. m1, m2, m3, m4 = log.message.match(/^Completed (\d+) (.*) in (\d+\.?\d*)ms (.*)$/).captures
  64. http_code = m1.to_i
  65. duration = m3.to_f
  66. duration = duration > TOTAL_RENDERED_VIEW_DURATION ? " in #{m3}ms #{m4}" : ''
  67. log.message = "Completed #{BOLD}#{http_code}#{CLEAR} #{m2}#{duration}"
  68. if http_code / 100 == 2
  69. log.dimensions = AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
  70. elsif http_code / 100 == 3
  71. @memory = '║'
  72. log.dimensions = AnsiDimensions.new(rails: "#{@memory}")
  73. elsif http_code / 100 == 4
  74. log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
  75. elsif http_code / 100 == 5
  76. log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
  77. end
  78. elsif log.message =~ /^(Processing|Parameters)/
  79. log.level = :debug
  80. if log.message =~ /^Processing/
  81. log.message = two_captures_last_as_bold(log.message, /(^Processing by \w*#\w* as )(.*)/)
  82. end
  83. elsif log.message =~ /Redirected/
  84. log.level = :debug
  85. log.message = two_captures_last_as_bold(log.message,
  86. /^(Redirected to )#{Rails.application.routes.url_helpers.root_url.chop}(.*)/)
  87. end
  88. elsif log.exception
  89. log.dimensions = AnsiDimensions.new(
  90. rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
  91. after: 1,
  92. terminus: true
  93. )
  94. end
  95. elsif log.name =~ /^(ActionView|ActiveRecord)::Base/
  96. log.level = :debug
  97. log.message.lstrip!
  98. if log.name == 'ActiveRecord::Base' && log.message.starts_with?('↳ ')
  99. log.message = AnsiCommon.ansi_trace(log.message)
  100. elsif log.name == 'ActionView::Base'
  101. match = log.message.match(/^Rendered( layout| collection of|) (.*?\.erb)(.*)(\(Duration:.*\))/)
  102. if match
  103. m1, m2, m3, m4 = match.captures
  104. duration = m4.match(/Duration: (\d+\.?\d*)ms/).match(1).to_f
  105. duration = duration < RENDERED_VIEW_DURATION ? '' : m4.to_s
  106. log.message = "Rendered#{m1} #{m2}#{m3}#{duration}"
  107. end
  108. end
  109. end
  110. log
  111. end
  112. def draw_fatal(char = CHAR_FATAL)
  113. AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
  114. end
  115. def origin = colorize(centerize(log.name), TEXT_CYAN)
  116. def build_prefix(char) = "#{origin}#{colorize(char)}"
  117. def build_terminus = "#{origin} #{TERMINUS_STRING} "
  118. def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
  119. def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
  120. def stackisize(items)
  121. return '' if items.empty?
  122. traces = items.map { |item| AnsiCommon.ansi_trace(item) }
  123. "\n#{traces.join("\n")}"
  124. end
  125. def build_dimensions(dimensions)
  126. "#{origin} #{dimensions.rails} "
  127. end
  128. def compute_useful_length
  129. IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
  130. rescue StandardError
  131. FAILOVER_WRAP
  132. end
  133. def message
  134. colorize(log.message) if log.message
  135. end
  136. def exception
  137. return unless log.exception
  138. exc = log.exception
  139. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  140. message = colorize(exc.message.chomp(''), color_map[:error])
  141. backtrace = stackisize(Rails.backtrace_cleaner.clean(exc.backtrace))
  142. "#{clazz}#{message}#{backtrace}"
  143. end
  144. def level_char
  145. case log.level
  146. when :info, :debug then ' '
  147. else log.level.to_s.chr.upcase
  148. end
  149. end
  150. def wrap_level(length, *items)
  151. prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
  152. continuation = build_prefix('┆')
  153. result = items.map do |item|
  154. AnsiWrapper.wrap(item, length, prefix, continuation)
  155. end
  156. if log.dimensions&.terminus
  157. terminus = AnsiWrapper.wrap(' ', length, build_terminus)
  158. result << terminus
  159. end
  160. log.dimensions&.before&.times { result.unshift('') }
  161. log.dimensions&.after&.times { result << '' }
  162. result.compact.join("\n")
  163. end
  164. end