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.

55 lines
1.5 KiB

3 weeks ago
  1. module Semantic
  2. # Abstract colorized formatter
  3. class AbstractFormatter < SemanticLogger::Formatters::Color
  4. include AnsiColors
  5. TAG_NONE = ''.freeze
  6. CENTER_SIZE = 20
  7. TAG_FIXED_LENGTH = Rails.application.config.x.action_controller.main_session_tag_fixed_length || 10
  8. def initialize(color_map:, time_format: nil)
  9. super(color_map:)
  10. @time_format = time_format
  11. end
  12. private
  13. def exception
  14. return unless log.exception
  15. if log.exception.is_a?(Semantic::TagWrapError)
  16. exc_wrapper = log.exception
  17. # puts "TAG_WRAP detected #{exc_wrapper.tag}"
  18. exc = exc_wrapper.error
  19. log.tags = Array.wrap(exc_wrapper.tag)
  20. else
  21. exc = log.exception
  22. end
  23. clazz = colorize("#{exc.class}\n", color_map[:fatal])
  24. message = colorize(exc.message.chomp(''), color_map[:error])
  25. backtrace = exc.backtrace
  26. stack = Semantic::Helper.stackisize(*backtrace)
  27. ["#{clazz}#{message}", stack].compact.join("\n")
  28. end
  29. def origin
  30. true_class = log.name == log.name.upcase_first
  31. taint = true_class ? TEXT_CYAN : TEXT_GRAY_400
  32. colorize(centerize(log.name), taint)
  33. end
  34. def tags
  35. first_tag, taint = log.tags.empty? ? [TAG_NONE, CLEAR] : [log.tags.first, BG_GRAY + TEXT_YELLOW]
  36. colorize(centerize(first_tag, TAG_FIXED_LENGTH), taint)
  37. end
  38. def named_tags
  39. return if log.named_tags.empty?
  40. log.named_tags
  41. end
  42. def centerize(text, max_length = CENTER_SIZE) = text.reverse.truncate(max_length).reverse.center(max_length)
  43. end
  44. end