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.

59 lines
2.0 KiB

1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month ago
1 month 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. end
  14. end
  15. private
  16. def once_and_reload(&)
  17. yield
  18. Rails.autoloaders.main.on_load('ApplicationController', &)
  19. end
  20. def append_ansi_formatter
  21. SemanticLogger.clear_appenders!
  22. formatter = Semantic::AnsiFormatter.new
  23. SemanticLogger.add_appender(io: $stdout,
  24. formatter:,
  25. filter: ->(log) { !formatter.reject(log) })
  26. end
  27. def register_action_controller
  28. @log_subscriber = Semantic::Subscribers::ActionController.new(@session_key)
  29. register_action_controller_hook(:start_processing)
  30. register_action_controller_hook(:process_action, :finish_processing)
  31. register_action_controller_hook(:redirect_to)
  32. %i[send_file send_data halted_callback unpermitted_parameters send_stream write_fragment
  33. read_fragment expire_fragment exist_fragment?].each do |hook|
  34. register_action_controller_hook(hook, :any_hook)
  35. end
  36. end
  37. def register_action_controller_hook(hook, method = hook)
  38. @subscribers << ActiveSupport::Notifications.subscribe("#{hook}.action_controller") do |event|
  39. @log_subscriber.send(method, event)
  40. end
  41. end
  42. def reset_subscribers
  43. if defined?(@subscribers)
  44. @subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
  45. @subscribers.clear
  46. else
  47. @subscribers = []
  48. end
  49. end
  50. end
  51. end