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.

174 lines
5.1 KiB

6 months ago
5 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
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. def initialize
  17. super(color_map:
  18. ColorMap.new(
  19. debug: CLEAR + TEXT_GRAY_400,
  20. info: CLEAR + TEXT_GRAY_100,
  21. warn: CLEAR + BG_YELLOW + TEXT_BLACK,
  22. error: CLEAR + BG_RED + TEXT_WHITE,
  23. fatal: CLEAR + BG_MAGENTA + BOLD + TEXT_WHITE
  24. ))
  25. @memory = nil
  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. return true if log.name == 'ActionView::Base' && log.message&.starts_with?(' Rendering')
  36. true if log.name == 'Rails' && log.message&.starts_with?('Loaded')
  37. end
  38. private
  39. def two_captures_last_as_bold(message, regex)
  40. match = message.match(regex)
  41. return "unmatched: #{message}" unless match
  42. m1, m2 = match.captures
  43. "#{m1}#{BOLD}#{m2}#{CLEAR}"
  44. end
  45. def alter(log)
  46. if log.name == 'Rails'
  47. if log.message
  48. log.message.lstrip!
  49. log.message.chomp!('')
  50. if log.message.starts_with?('Started')
  51. rails = '╓─╖'
  52. before = 1
  53. if @memory
  54. rails = "#{@memory}"
  55. before = 0
  56. end
  57. log.dimensions = AnsiDimensions.new(rails:, before:)
  58. @memory = nil
  59. log.message = two_captures_last_as_bold(log.message, /(^Started \w* )"(.*?)"/)
  60. elsif log.message.starts_with?('Completed 2')
  61. log.dimensions = AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
  62. elsif log.message.starts_with?('Completed 3')
  63. @memory = '║'
  64. log.dimensions = AnsiDimensions.new(rails: "#{@memory}")
  65. elsif log.message.starts_with?('Completed 4')
  66. log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
  67. elsif log.message.starts_with?('Completed 5')
  68. log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
  69. elsif log.message =~ /^(Processing|Parameters)/
  70. log.level = :debug
  71. if log.message =~ /^Processing/
  72. log.message = two_captures_last_as_bold(log.message, /(^Processing by \w*#\w* as )(.*)/)
  73. end
  74. elsif log.message =~ /Redirected/
  75. log.message = two_captures_last_as_bold(log.message, /^(Redirected to )http:.*:\d+(.*)/)
  76. end
  77. elsif log.exception
  78. log.dimensions = AnsiDimensions.new(
  79. rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
  80. after: 1,
  81. terminus: true
  82. )
  83. end
  84. elsif log.name =~ /^(ActionView|ActiveRecord)::Base/
  85. log.level = :debug
  86. log.message.lstrip!
  87. if log.name == 'ActiveRecord::Base' && log.message.starts_with?('↳ ')
  88. log.message = AnsiCommon.ansi_trace(log.message)
  89. end
  90. end
  91. log
  92. end
  93. def draw_fatal(char = CHAR_FATAL)
  94. AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
  95. end
  96. def origin = colorize(centerize(log.name), TEXT_CYAN)
  97. def build_prefix(char) = "#{origin}#{colorize(char)}"
  98. def build_terminus = "#{origin} #{TERMINUS_STRING} "
  99. def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
  100. def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
  101. def stackisize(items)
  102. return '' if items.empty?
  103. traces = items.map { |item| AnsiCommon.ansi_trace(item) }
  104. "\n#{traces.join("\n")}"
  105. end
  106. def build_dimensions(dimensions)
  107. "#{origin} #{dimensions.rails} "
  108. end
  109. def compute_useful_length
  110. IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
  111. rescue StandardError
  112. FAILOVER_WRAP
  113. end
  114. def message
  115. colorize(log.message) if log.message
  116. end
  117. def exception
  118. return unless log.exception
  119. exc = log.exception
  120. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  121. message = colorize(exc.message.chomp(''), color_map[:error])
  122. backtrace = stackisize(Rails.backtrace_cleaner.clean(exc.backtrace))
  123. "#{clazz}#{message}#{backtrace}"
  124. end
  125. def level_char
  126. case log.level
  127. when :info, :debug then ' '
  128. else log.level.to_s.chr.upcase
  129. end
  130. end
  131. def wrap_level(length, *items)
  132. prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
  133. continuation = build_prefix('┆')
  134. result = items.map do |item|
  135. AnsiWrapper.wrap(item, length, prefix, continuation)
  136. end
  137. if log.dimensions&.terminus
  138. terminus = AnsiWrapper.wrap(' ', length, build_terminus)
  139. result << terminus
  140. end
  141. log.dimensions&.before&.times { result.unshift('') }
  142. log.dimensions&.after&.times { result << '' }
  143. result.compact.join("\n")
  144. end
  145. end