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.

78 lines
2.2 KiB

9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 months ago
9 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_DEBUG = "\e[90m".freeze
  7. ANSI_INFO = SemanticLogger::AnsiColors::GREEN
  8. ANSI_WARN = SemanticLogger::AnsiColors::YELLOW
  9. ANSI_ERROR = SemanticLogger::AnsiColors::RED
  10. ANSI_FATAL = SemanticLogger::AnsiColors::MAGENTA
  11. def initialize
  12. super(time_format: nil,
  13. color_map: ColorMap.new(
  14. debug: ANSI_DEBUG,
  15. info: ANSI_INFO,
  16. warn: ANSI_WARN,
  17. fatal: ANSI_ERROR
  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. "#{ANSI_DEBUG}#{log.name.rjust(20)}#{color_map.clear}"
  43. end
  44. def process_info
  45. fname = file_name_and_line
  46. "#{color}[#{fname}]#{color_map.clear}" if fname
  47. end
  48. def exception
  49. return unless log.exception
  50. root_path = Rails.root.to_s
  51. backtrace = log.exception.backtrace.select do |line|
  52. line.starts_with?(root_path)
  53. end
  54. if backtrace.count.positive?
  55. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear} #{color}#{log.exception.message}\n#{color}#{backtrace.join("\n")}#{color_map.clear}\n\n"
  56. else
  57. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n"
  58. end
  59. end
  60. def call(log, logger)
  61. self.color = color_map[log.level]
  62. self.log = log
  63. self.logger = logger
  64. [name, level, tags, named_tags, duration, message, payload, exception].compact.join(' ')
  65. end
  66. end