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.

79 lines
2.5 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
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. @time_format = nil if File.exist?(File.join(Rails.root, "Procfile.dev"))
  15. end
  16. def time
  17. "#{color}#{format_time(log.time)}#{color_map.clear}" if time_format
  18. end
  19. def message
  20. return unless log.message
  21. prefix = "#{color}--#{color_map.clear}"
  22. case log.level
  23. when :info
  24. message = log.message
  25. message = message&.rstrip if log.name == "Rails" && message.starts_with?("Completed")
  26. message = message.split("for")[0] if log.name == "Rails" && message.starts_with?("Started")
  27. if log.name == "Rails" || log.name == "ActionView::Base"
  28. "#{prefix} #{ANSI_GRAY}#{message}#{color_map.clear}"
  29. else
  30. "#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{message}#{color_map.clear}"
  31. end
  32. when :warn
  33. "#{prefix} #{ANSI_REVERSED_WARNING}#{log.message}#{color_map.clear}"
  34. when :error, :fatal
  35. "#{prefix} #{ANSI_REVERSED_ERROR}#{log.message}#{color_map.clear}"
  36. else
  37. "#{prefix} #{color}#{log.message}#{color_map.clear}"
  38. end
  39. end
  40. def tags; end
  41. def process_info
  42. fname = file_name_and_line
  43. "#{color}[#{fname}]#{color_map.clear}" if fname
  44. end
  45. def exception
  46. return unless log.exception
  47. root_path = Rails.root.to_s
  48. backtrace = log.exception.backtrace.select do |line|
  49. line.starts_with?(root_path)
  50. end
  51. if backtrace.count.positive?
  52. "-- #{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"
  53. else
  54. "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n"
  55. end
  56. end
  57. def call(log, logger)
  58. self.color = color_map[log.level]
  59. self.log = log
  60. self.logger = logger
  61. if @time_format
  62. [time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
  63. else
  64. [tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
  65. end
  66. end
  67. end