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.

68 lines
2.0 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years 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_GRAY = "\e[90m".freeze
  6. # Return the complete log level name in uppercase
  7. def initialize
  8. super(time_format: "%H:%M:%S",
  9. color_map: ColorMap.new(
  10. debug: ANSI_GRAY,
  11. info: SemanticLogger::AnsiColors::GREEN,
  12. warn: SemanticLogger::AnsiColors::YELLOW,
  13. ))
  14. end
  15. def time
  16. "#{color}#{format_time(log.time)}#{color_map.clear}" if time_format
  17. end
  18. def message
  19. return unless log.message
  20. prefix = "#{color}--#{color_map.clear}"
  21. case log.level
  22. when :info
  23. message = if log.name == "Rails" && log.message.starts_with?("Started")
  24. log.message.split("for")[0]
  25. else
  26. log.message
  27. end
  28. if log.name == "Rails" || log.name == "ActionView::Base"
  29. "#{prefix} #{ANSI_GRAY}#{message}#{color_map.clear}"
  30. else
  31. "#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{message}#{color_map.clear}"
  32. end
  33. when :warn
  34. "#{prefix} #{ANSI_REVERSED_WARNING}#{log.message}#{color_map.clear}"
  35. when :error, :fatal
  36. "#{prefix} #{ANSI_REVERSED_ERROR}#{log.message}#{color_map.clear}"
  37. else
  38. "#{prefix} #{color}#{log.message}#{color_map.clear}"
  39. end
  40. end
  41. def tags; end
  42. def process_info
  43. fname = file_name_and_line
  44. "#{color}[#{fname}]#{color_map.clear}" if fname
  45. end
  46. def exception
  47. return unless log.exception
  48. root_path = Rails.root.to_s
  49. backtrace = log.exception.backtrace.select do |line|
  50. line.starts_with?(root_path)
  51. end
  52. if backtrace.count.positive?
  53. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear} #{color}#{log.exception.message}#{color_map.clear}#{SemanticLogger::AnsiColors::WHITE}\n\t#{backtrace.join("\n\t")}#{color_map.clear}\n\n"
  54. else
  55. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n"
  56. end
  57. end
  58. end