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.

158 lines
4.8 KiB

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