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.

77 lines
2.2 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. # My Custom colorized formatter
  2. class BasicFormatter < SemanticLogger::Formatters::Color
  3. ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze
  4. ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze
  5. ANSI_REVERSED_FATAL = "\e[1;30;41m".freeze
  6. ANSI_GRAY = "\e[90m".freeze
  7. ANSI_GREEN = SemanticLogger::AnsiColors::GREEN
  8. ANSI_YELLOW = SemanticLogger::AnsiColors::YELLOW
  9. ANSI_RED = SemanticLogger::AnsiColors::RED
  10. ANSI_MAGENTA = SemanticLogger::AnsiColors::MAGENTA
  11. def initialize
  12. super(time_format: nil,
  13. color_map: ColorMap.new(
  14. debug: ANSI_GRAY,
  15. info: ANSI_GREEN,
  16. warn: ANSI_YELLOW,
  17. fatal: ANSI_RED
  18. ))
  19. end
  20. def message
  21. return unless log.message
  22. msg = log.message
  23. prefix = "#{color} #{color_map.clear}"
  24. case log.level
  25. when :info
  26. "#{prefix}#{SemanticLogger::AnsiColors::WHITE}#{msg}#{color_map.clear}"
  27. when :warn
  28. "#{prefix}#{ANSI_REVERSED_WARNING}#{msg}#{color_map.clear}"
  29. when :error
  30. "#{prefix}#{ANSI_REVERSED_ERROR}#{msg}#{color_map.clear}"
  31. when :fatal
  32. "#{prefix}#{ANSI_REVERSED_FATAL}#{msg}#{color_map.clear}"
  33. else
  34. "#{prefix}#{color}#{msg}#{color_map.clear}"
  35. end
  36. end
  37. def level
  38. "#{color}#{log.level.to_s.chr.upcase}#{color_map.clear}"
  39. end
  40. def name
  41. "#{color}#{log.name.ljust(20)}#{color_map.clear}"
  42. end
  43. def process_info
  44. fname = file_name_and_line
  45. "#{color}[#{fname}]#{color_map.clear}" if fname
  46. end
  47. def exception
  48. return unless log.exception
  49. root_path = Rails.root.to_s
  50. backtrace = log.exception.backtrace.select do |line|
  51. line.starts_with?(root_path)
  52. end
  53. if backtrace.count.positive?
  54. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear} #{color}#{log.exception.message}\n#{color}#{backtrace.join("\n")}#{color_map.clear}\n\n"
  55. else
  56. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n"
  57. end
  58. end
  59. def call(log, logger)
  60. self.color = color_map[log.level]
  61. self.log = log
  62. self.logger = logger
  63. [name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ')
  64. end
  65. end