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.

159 lines
4.8 KiB

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