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.

179 lines
5.4 KiB

3 months ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
2 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. require 'io/console'
  2. require 'amazing_print'
  3. require 'json'
  4. module Semantic
  5. # wraps meanwhile takes care of ansi colors
  6. class FancyFormatter < AbstractFormatter
  7. TAG_NONE = ''.freeze
  8. CENTER_SIZE = 20
  9. FOREMAN_PREFIX_LENGTH = 18
  10. FAILOVER_WRAP = 80
  11. CHAR_FATAL = '⯶'.freeze
  12. TERMINUS_STRING = '╙─╜'.freeze
  13. RENDERED_VIEW_DURATION = 100
  14. TOTAL_RENDERED_VIEW_DURATION = RENDERED_VIEW_DURATION * 5
  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. end
  26. def call(log, logger)
  27. # puts 'ok2'
  28. log = alter(log)
  29. self.log = log
  30. self.logger = logger
  31. self.color = color_map[log.level]
  32. computed_exception = exception
  33. result = wrap_level(compute_useful_length, message, payload, computed_exception)
  34. return result unless computed_exception
  35. result << "\n" << build_terminus
  36. rescue StandardError => e
  37. puts "Error during formatting: #{e.message}"
  38. puts e.backtrace.join("\n")
  39. end
  40. def reject(log)
  41. return true if log.name == 'ActionView::Base' && log.message&.starts_with?(' Rendering')
  42. true if log.name == 'Rails' && log.message&.starts_with?('Loaded')
  43. end
  44. private
  45. def tags
  46. first_tag, taint = log.tags.empty? ? [TAG_NONE, CLEAR] : [log.tags.first, BG_GRAY + DARK_TEXT_YELLOW]
  47. colorize(centerize(first_tag, 10), taint)
  48. end
  49. def named_tags
  50. return if log.named_tags.empty?
  51. log.named_tags
  52. end
  53. def build_regex_redirected
  54. dev_port = Rails.application.routes.default_url_options[:port]
  55. dev_hosts = ['127.0.0.1', 'localhost', Rails.application.routes.default_url_options[:host]].uniq.join('|')
  56. dev_from = "http://(?:#{dev_hosts}):#{dev_port}"
  57. regex_s = "^(Redirected to )#{dev_from}(.*)"
  58. Regexp.new(regex_s)
  59. end
  60. def two_captures_last_as_bold(message, regex)
  61. match = message.match(regex)
  62. return "unmatched: #{message}" unless match
  63. m1, m2 = match.captures
  64. "#{m1}#{BOLD}#{m2}#{CLEAR}"
  65. end
  66. def alter(log)
  67. if log.name =~ /^(ActionView|ActiveRecord)::Base/
  68. log.level = :debug
  69. log.message.lstrip!
  70. if log.name == 'ActiveRecord::Base'
  71. unbold!(log.message)
  72. if log.message.starts_with?('↳ ')
  73. log.message = ansi_trace(log.message, '⇄')
  74. else
  75. sql_match = log.message.match(/\s+\[\[.*\]\]$/)
  76. if sql_match
  77. sql_command = sql_match.pre_match
  78. sql_type, sql_entry = sql_command.split("\e[0m")
  79. sql_color_entry = sql_entry.match(ANSI_REGEX).to_s
  80. sql_args = JSON.parse(sql_match.to_s).map(&:last)
  81. sql_args.each_with_index do |val, index|
  82. sql_arg = val.inspect
  83. sql_arg = "'#{val.gsub("'", "''")}'" if val.is_a?(String)
  84. sql_entry.gsub!("$#{index + 1}", colorize(sql_arg, BOLD) + sql_color_entry)
  85. end
  86. log.message = [sql_type, sql_entry].join(CLEAR)
  87. end
  88. end
  89. elsif log.name == 'ActionView::Base'
  90. match = log.message.match(/^Rendered( layout| collection of|) (.*?\.erb)(.*)(\(Duration:.*\))/)
  91. if match
  92. m1, m2, m3, m4 = match.captures
  93. duration = m4.match(/Duration: (\d+\.?\d*)ms/).match(1).to_f
  94. duration = duration < RENDERED_VIEW_DURATION ? '' : m4.to_s
  95. log.message = "⇤ Rendered#{m1} app/views/#{m2}#{m3}#{duration}"
  96. end
  97. end
  98. end
  99. log
  100. end
  101. def draw_fatal(char = CHAR_FATAL) = BG_MAGENTA + BOLD + TEXT_WHITE + char + CLEAR
  102. def build_prefix(char) = "#{tags} #{origin}#{colorize(char)}"
  103. def build_terminus = "#{tags} #{origin} #{TERMINUS_STRING} "
  104. def centerize(text, max_length = CENTER_SIZE) = text.reverse.truncate(max_length).reverse.center(max_length)
  105. def origin
  106. true_class = log.name == log.name.upcase_first
  107. taint = true_class ? TEXT_CYAN : TEXT_GRAY_400
  108. colorize(centerize(log.name), taint)
  109. end
  110. def build_dimensions(dimensions)
  111. "#{tags} #{origin} #{dimensions.rails} "
  112. end
  113. def compute_useful_length
  114. IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
  115. rescue StandardError
  116. FAILOVER_WRAP
  117. end
  118. def unbold!(text) = text.gsub!(BOLD, '')
  119. def message
  120. colorize(log.message) if log.message
  121. end
  122. def payload
  123. return unless log.payload
  124. log.payload.ai(indent: 2, object_id: false)
  125. end
  126. def level_char
  127. case log.level
  128. when :info, :debug then ' '
  129. else log.level.to_s.chr.upcase
  130. end
  131. end
  132. def wrap_level(length, *items)
  133. prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
  134. continuation = build_prefix('┆')
  135. result = items.map do |item|
  136. Semantic::AnsiWrapper.wrap(item, length, prefix, continuation)
  137. end
  138. if log.dimensions&.terminus
  139. terminus = Semantic::AnsiWrapper.wrap(' ', length, build_terminus)
  140. result << terminus
  141. end
  142. log.dimensions&.before&.times { result.unshift('') }
  143. log.dimensions&.after&.times { result << '' }
  144. result.compact.join("\n")
  145. end
  146. end
  147. end