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.2 KiB

3 months ago
2 months ago
2 months ago
2 months ago
3 months ago
2 months ago
3 months ago
3 months ago
2 months ago
3 months ago
3 months ago
2 months ago
2 months ago
3 months ago
3 months ago
2 months ago
2 months ago
2 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. RailsSemanticLogger::ActionController::LogSubscriber.logger.level = :fatal # useful for remanent Rack::Log started
  7. once_and_reload do
  8. Semantic::NotificationUtil.clear_subscribers(/\.action_controller$/)
  9. Semantic::NotificationUtil.clear_subscribers(/\.action_view$/)
  10. append_ansi_formatter
  11. reset_subscribers
  12. register_action_controller
  13. register_action_view
  14. end
  15. end
  16. private
  17. def once_and_reload(&)
  18. yield
  19. Rails.autoloaders.main.on_load('ApplicationController', &)
  20. end
  21. def append_ansi_formatter
  22. SemanticLogger.clear_appenders!
  23. formatter = Semantic::AnsiFormatter.new
  24. SemanticLogger.add_appender(io: $stdout,
  25. formatter:,
  26. filter: ->(log) { !formatter.reject(log) })
  27. end
  28. def register_action_controller
  29. sub_instance = Semantic::Subscribers::ActionController.new(@session_key)
  30. register_hook(sub_instance, :start_processing)
  31. register_hook(sub_instance, :process_action, :finish_processing)
  32. register_hook(sub_instance, :redirect_to)
  33. %i[send_file send_data halted_callback unpermitted_parameters send_stream write_fragment
  34. read_fragment expire_fragment exist_fragment?].each do |hook|
  35. register_hook(sub_instance, hook, :any_hook)
  36. end
  37. end
  38. def register_action_view
  39. sub_instance = Semantic::Subscribers::ActionView.new
  40. %i[render_template render_partial render_collection render_layout].each do |hook|
  41. register_hook(sub_instance, hook)
  42. end
  43. end
  44. def register_hook(sub_instance, hook, method = hook)
  45. @subscribers << ActiveSupport::Notifications.subscribe("#{hook}.#{sub_instance.event_group}") do |event|
  46. sub_instance.send(method, event)
  47. end
  48. end
  49. def reset_subscribers
  50. if defined?(@subscribers)
  51. @subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
  52. @subscribers.clear
  53. else
  54. @subscribers = []
  55. end
  56. end
  57. end
  58. end