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.5 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
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. # puts caller_locations(1, 15)
  29. log = alter(log)
  30. self.log = log
  31. self.logger = logger
  32. self.color = color_map[log.level]
  33. wrap_level(compute_useful_length, message, payload, exception)
  34. end
  35. def reject(log)
  36. true if log.name == 'ActionView::Base' && log.message.starts_with?(' Rendering')
  37. end
  38. private
  39. def alter(log)
  40. if log.name == 'Rails'
  41. if log.message
  42. log.message.lstrip!
  43. log.message.chomp!('')
  44. if log.message.starts_with?('Started')
  45. rails = '╓─╖'
  46. before = 1
  47. if @memory
  48. rails = "#{@memory}"
  49. before = 0
  50. end
  51. log.dimensions = AnsiDimensions.new(rails:, before:)
  52. @memory = nil
  53. elsif log.message.starts_with?('Completed 2')
  54. log.dimensions = AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
  55. elsif log.message.starts_with?('Completed 3')
  56. @memory = '║'
  57. log.dimensions = AnsiDimensions.new(rails: "#{@memory}")
  58. elsif log.message.starts_with?('Completed 4')
  59. log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
  60. elsif log.message.starts_with?('Completed 5')
  61. log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
  62. elsif log.message =~ /^(Processing|Parameters)/
  63. log.level = :debug
  64. end
  65. elsif log.exception
  66. log.dimensions = AnsiDimensions.new(
  67. rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
  68. after: 1,
  69. terminus: true
  70. )
  71. end
  72. elsif log.name =~ /^(ActionView|ActiveRecord)::Base/
  73. log.level = :debug
  74. log.message.lstrip!
  75. if log.name == 'ActiveRecord::Base' && log.message.starts_with?('↳ ')
  76. log.message = AnsiCommon.ansi_trace(log.message)
  77. end
  78. end
  79. log
  80. end
  81. def draw_fatal(char = CHAR_FATAL)
  82. AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
  83. end
  84. def origin = colorize(centerize(log.name), TEXT_CYAN)
  85. def build_prefix(char) = "#{origin}#{colorize(char)}"
  86. def build_terminus = "#{origin} #{TERMINUS_STRING} "
  87. def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
  88. def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
  89. def stackisize(items)
  90. return '' if items.empty?
  91. traces = items.map { |item| AnsiCommon.ansi_trace(item) }
  92. "\n#{traces.join("\n")}"
  93. end
  94. def build_dimensions(dimensions)
  95. "#{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 message
  103. colorize(log.message) if log.message
  104. end
  105. def exception
  106. return unless log.exception
  107. exc = log.exception
  108. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  109. message = colorize(exc.message.chomp(''), color_map[:error])
  110. backtrace = stackisize(Rails.backtrace_cleaner.clean(exc.backtrace))
  111. "#{clazz}#{message}#{backtrace}"
  112. end
  113. def level_char
  114. case log.level
  115. when :info, :debug then ' '
  116. else log.level.to_s.chr.upcase
  117. end
  118. end
  119. def wrap_level(length, *items)
  120. prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
  121. continuation = build_prefix('┆')
  122. result = items.map do |item|
  123. AnsiWrapper.wrap(item, length, prefix, continuation)
  124. end
  125. if log.dimensions&.terminus
  126. terminus = AnsiWrapper.wrap(' ', length, build_terminus)
  127. result << terminus
  128. end
  129. log.dimensions&.before&.times { result.unshift('') }
  130. log.dimensions&.after&.times { result << '' }
  131. result.compact.join("\n")
  132. end
  133. end