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.

46 lines
1.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
  1. module Semantic
  2. # use the Zeitwerk autoloader to reattach_appender for development autoreloading feature
  3. class DevLoader
  4. def initialize
  5. @subscribers = []
  6. force_preload_module
  7. Rails.autoloaders.main.on_load('ApplicationController') do
  8. append_ansi_formatter
  9. register_log_subscriber
  10. end
  11. append_ansi_formatter
  12. # FIXME: when final
  13. # Rails.logger.level = :error
  14. end
  15. private
  16. def force_preload_module
  17. self.class.module_parent.constants.each do |const|
  18. self.class.module_parent.const_get(const)
  19. end
  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_log_subscriber
  29. @subscribers.each { |sub| ActiveSupport::Notifications.unsubscribe(sub) }
  30. @subscribers.clear
  31. log_subscriber = Semantic::LogSubscriber.new('toto1')
  32. @subscribers << ActiveSupport::Notifications.subscribe('start_processing.action_controller') do |event|
  33. log_subscriber.start_processing(event)
  34. end
  35. @subscribers << ActiveSupport::Notifications.subscribe('process_action.action_controller') do |event|
  36. log_subscriber.finish_processing(event)
  37. end
  38. end
  39. end
  40. end