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.

81 lines
2.4 KiB

1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year ago
1 year 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 = if log.name == "Rails" && log.message.starts_with?("Started")
  25. log.message.split("for")[0]
  26. else
  27. log.message
  28. end
  29. if log.name == "Rails" || log.name == "ActionView::Base"
  30. "#{prefix} #{ANSI_GRAY}#{message}#{color_map.clear}"
  31. else
  32. "#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{message}#{color_map.clear}"
  33. end
  34. when :warn
  35. "#{prefix} #{ANSI_REVERSED_WARNING}#{log.message}#{color_map.clear}"
  36. when :error, :fatal
  37. "#{prefix} #{ANSI_REVERSED_ERROR}#{log.message}#{color_map.clear}"
  38. else
  39. "#{prefix} #{color}#{log.message}#{color_map.clear}"
  40. end
  41. end
  42. def tags; 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}#{color_map.clear}#{SemanticLogger::AnsiColors::WHITE}\n\t#{backtrace.join("\n\t")}#{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. if @time_format
  64. [time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
  65. else
  66. [tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
  67. end
  68. end
  69. end