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.

149 lines
4.1 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. $memory = nil
  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. end
  26. def call(log, logger)
  27. log = alter(log)
  28. self.log = log
  29. self.logger = logger
  30. self.color = color_map[log.level]
  31. wrap_level(compute_useful_length, message, payload, exception)
  32. end
  33. def reject(log)
  34. log.name == 'ScoresController' || false
  35. false
  36. end
  37. private
  38. def alter(log)
  39. if log.name == 'Rails'
  40. if log.message
  41. log.message.chomp!('')
  42. if log.message.starts_with?('Started')
  43. rails = '╓─╖'
  44. before = 1
  45. if $memory
  46. rails = "#{$memory}"
  47. before = 0
  48. end
  49. log.dimensions = AnsiDimensions.new(rails:, before:)
  50. $memory = nil
  51. elsif log.message.starts_with?('Completed 2')
  52. log.dimensions = AnsiDimensions.new(rails: TERMINUS_STRING, after: 1)
  53. elsif log.message.starts_with?('Completed 3')
  54. $memory = '║'
  55. log.dimensions = AnsiDimensions.new(rails: "#{$memory}")
  56. elsif log.message.starts_with?('Completed 4')
  57. log.dimensions = AnsiDimensions.new(rails: '╙╨╜')
  58. elsif log.message.starts_with?('Completed 5')
  59. log.dimensions = AnsiDimensions.new(rails: "#{draw_fatal}")
  60. end
  61. elsif log.exception
  62. log.dimensions = AnsiDimensions.new(
  63. rails: "#{draw_fatal(log.level.to_s.chr.upcase)}",
  64. after: 1,
  65. terminus: true
  66. )
  67. end
  68. end
  69. log
  70. end
  71. def draw_fatal(char = CHAR_FATAL)
  72. AnsiColors::BG_MAGENTA + AnsiColors::BOLD + AnsiColors::TEXT_WHITE + char + AnsiColors::CLEAR
  73. end
  74. def origin = colorize(centerize(log.name), TEXT_CYAN)
  75. def build_prefix(char) = "#{origin}#{colorize(char)}"
  76. def build_terminus = "#{origin} #{TERMINUS_STRING} "
  77. def centerize(text) = text.truncate(CENTER_SIZE).center(CENTER_SIZE)
  78. def colorize(text, tint = color) = "#{tint}#{text}#{CLEAR}"
  79. def stackisize(items) = items.length.positive? ? "\n#{items.join("\n")}" : ''
  80. def build_dimensions(dimensions)
  81. "#{origin} #{dimensions.rails} "
  82. end
  83. def compute_useful_length
  84. IO.console.winsize[1] - FOREMAN_PREFIX_LENGTH
  85. rescue StandardError
  86. FAILOVER_WRAP
  87. end
  88. def message
  89. colorize(log.message) if log.message
  90. end
  91. def exception
  92. return unless log.exception
  93. exc = log.exception
  94. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  95. message = colorize(exc.message.chomp(''), color_map[:error])
  96. backtrace = stackisize(clean_backtrace(exc.backtrace))
  97. "#{clazz}#{message}#{backtrace}"
  98. end
  99. def clean_backtrace(backtrace)
  100. root_path = Rails.root.to_s
  101. backtrace = backtrace.select { |line| line.starts_with?(root_path) }
  102. backtrace.map { |line| line.delete_prefix("#{root_path}/") }
  103. end
  104. def level_char
  105. case log.level
  106. when :info, :debug then ' '
  107. else log.level.to_s.chr.upcase
  108. end
  109. end
  110. def wrap_level(length, *items)
  111. prefix = log.dimensions ? build_dimensions(log.dimensions) : build_prefix(level_char)
  112. continuation = build_prefix('┆')
  113. result = items.map do |item|
  114. AnsiWrapper.wrap(item, length, prefix, continuation)
  115. end
  116. if log.dimensions&.terminus
  117. terminus = AnsiWrapper.wrap(' ', length, build_terminus)
  118. result << terminus
  119. end
  120. log.dimensions&.before&.times { result.unshift('') }
  121. log.dimensions&.after&.times { result << '' }
  122. result.compact.join("\n")
  123. end
  124. end