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.

155 lines
4.4 KiB

6 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
6 months ago
5 months ago
6 months ago
5 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 'io/console'
  5. require 'amazing_print'
  6. require 'json'
  7. # wraps meanwhile takes care of ansi colors
  8. class AnsiFormatter < SemanticLogger::Formatters::Color
  9. include AnsiColors
  10. CENTER_SIZE = 20
  11. FOREMAN_PREFIX_LENGTH = 18
  12. FAILOVER_WRAP = 80
  13. CHAR_FATAL = '⯶'.freeze
  14. TERMINUS_STRING = '╙─╜'.freeze
  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 caller_locations(1, 15)
  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. true if log.name == 'ActionView::Base' && log.message.starts_with?(' Rendering')
  36. end
  37. private
  38. def alter(log)
  39. if log.name == 'Rails'
  40. if log.message
  41. log.message.lstrip!
  42. log.message.chomp!('')
  43. if log.message.starts_with?('Started')
  44. rails = '╓─╖'
  45. before = 1
  46. if @memory
  47. rails = "#{@memory}"
  48. before = 0
  49. end
  50. log.dimensions = AnsiDimensions.new(rails:, before:)
  51. @memory = nil
  52. elsif log.message.starts_with?('Completed 2')
  53. log.dimensions = AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
  54. elsif log.message.starts_with?('Completed 3')
  55. @memory = '║'
  56. log.dimensions = AnsiDimensions.new(rails: "#{@memory}")
  57. elsif log.message.starts_with?('Completed 4')
  58. log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
  59. elsif log.message.starts_with?('Completed 5')
  60. log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
  61. elsif log.message =~ /^(Processing|Parameters)/
  62. log.level = :debug
  63. end
  64. elsif log.exception
  65. log.dimensions = AnsiDimensions.new(
  66. rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
  67. after: 1,
  68. terminus: true
  69. )
  70. end
  71. elsif log.name =~ /^(ActionView|ActiveRecord)::Base/
  72. log.level = :debug
  73. log.message.lstrip!
  74. end
  75. log
  76. end
  77. def draw_fatal(char = CHAR_FATAL)
  78. AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
  79. end
  80. def origin = colorize(centerize(log.name), TEXT_CYAN)
  81. def build_prefix(char) = "#{origin}#{colorize(char)}"
  82. def build_terminus = "#{origin} #{TERMINUS_STRING} "
  83. def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
  84. def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
  85. def stackisize(items) = items.length.positive? ? "\n#{items.join("\n")}" : ''
  86. def build_dimensions(dimensions)
  87. "#{origin} #{dimensions.rails} "
  88. end
  89. def compute_useful_length
  90. IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
  91. rescue StandardError
  92. FAILOVER_WRAP
  93. end
  94. def message
  95. colorize(log.message) if log.message
  96. end
  97. def exception
  98. return unless log.exception
  99. exc = log.exception
  100. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  101. message = colorize(exc.message.chomp(''), color_map[:error])
  102. backtrace = stackisize(clean_backtrace(exc.backtrace))
  103. "#{clazz}#{message}#{backtrace}"
  104. end
  105. def clean_backtrace(backtrace)
  106. root_path = Rails.root.to_s
  107. backtrace = backtrace.select { |line| line.starts_with?(root_path) }
  108. backtrace.map { |line| line.delete_prefix("#{root_path}/") }
  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. AnsiWrapper.wrap(item, length, prefix, continuation)
  121. end
  122. if log.dimensions&.terminus
  123. terminus = 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