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.

74 lines
2.4 KiB

3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
3 months ago
  1. module Semantic
  2. # use the Zeitwerk autoloader to reattach_appender for development autoreloading feature
  3. class DevLoader
  4. def initialize(session_key)
  5. @session_key = session_key
  6. force_preload_module
  7. once_and_reload do
  8. append_ansi_formatter
  9. register_log_subscriber
  10. end
  11. # FIXME: proper unsubscribe!!!
  12. RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal
  13. # RailsSemanticLogger.swap_subscriber(
  14. # RailsSemanticLogger::ActionController::LogSubscriber,
  15. # @log_subscriber, # attach missing!!!
  16. # :action_controller
  17. # )
  18. # ActiveSupport::LogSubscriber.subscribers.each do |sub|
  19. # puts "subscriber #{sub.pattern}"
  20. # end
  21. # RailsSemanticLogger.swap_subscriber(RailsSemanticLogger::ActionController::LogSubscriber,
  22. # @log_subscriber, :action_controller)
  23. end
  24. private
  25. def once_and_reload(&)
  26. yield
  27. Rails.autoloaders.main.on_load('ApplicationController', &)
  28. end
  29. def force_preload_module
  30. self.class.module_parent.constants.each { |const| self.class.module_parent.const_get(const) }
  31. end
  32. def append_ansi_formatter
  33. SemanticLogger.clear_appenders!
  34. formatter = Semantic::AnsiFormatter.new
  35. SemanticLogger.add_appender(io: $stdout,
  36. formatter:,
  37. filter: ->(log) { !formatter.reject(log) })
  38. end
  39. def register_log_subscriber
  40. reset_subscribers
  41. register_to_action_controller(:start_processing)
  42. register_to_action_controller(:process_action, :finish_processing)
  43. register_to_action_controller(:redirect_to)
  44. %i[send_file send_data halted_callback unpermitted_parameters send_stream write_fragment
  45. read_fragment expire_fragment exist_fragment?].each do |hook|
  46. register_to_action_controller(hook, :any_hook)
  47. end
  48. end
  49. def register_to_action_controller(hook, method = hook)
  50. @subscribers << ActiveSupport::Notifications.subscribe("#{hook}.action_controller") do |event|
  51. @log_subscriber.send(method, event)
  52. end
  53. end
  54. def reset_subscribers
  55. if defined?(@subscribers)
  56. @subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
  57. @subscribers.clear
  58. else
  59. @subscribers = []
  60. end
  61. @log_subscriber = Semantic::LogSubscriber.new(@session_key)
  62. end
  63. end
  64. end