@@ -55,12 +55,26 @@ def self.rails_6_1_higher?
5555 end
5656 end
5757
58+ # In Rails 7.1+, the server, console, and other components (like Sidekiq)
59+ # call Rails.logger.broadcast_to() to add additional log destinations.
60+ # This causes duplicate log lines and can lead to blocks being executed
61+ # multiple times (via BroadcastLogger's method_missing delegation).
62+ #
63+ # This initializer runs AFTER :initialize_logger (which wraps the logger
64+ # in a BroadcastLogger) but BEFORE any after_initialize hooks (where
65+ # Sidekiq and others add their loggers). This ensures the no-op is in
66+ # place before anyone tries to call broadcast_to.
67+ initializer 'epilog.prevent_broadcast' , after : :initialize_logger do
68+ if ::Rails . gem_version >= Gem ::Version . new ( '7.1' ) && ::Rails . logger
69+ ::Rails . logger . define_singleton_method ( :broadcast_to ) { |*| nil }
70+ end
71+ end
72+
5873 # In Rails 7.1+, ActionView::LogSubscriber::Start are attached late
5974 # in the initialization process, so we need to disable them after
6075 # initialization completes
6176 config . after_initialize do
6277 disable_rails_defaults
63- prevent_double_logs_from_broadcast
6478 end
6579
6680 class << self
@@ -101,21 +115,6 @@ def unsubscribe_action_view_start_listeners
101115 end
102116 end
103117
104- # In Rails 7.1+, the server and console call Rails.logger.broadcast_to(console)
105- # which adds a second logger and causes every log line to appear twice
106- # (e.g. custom format + default Logger format). Override broadcast_to with
107- # a no-op to prevent adding duplicate destinations.
108- #
109- # This must be done in after_initialize (not at require time) because:
110- # 1. Rails wraps the logger in a BroadcastLogger during bootstrap
111- # 2. We need to override the instance method on the actual Rails.logger
112- def prevent_double_logs_from_broadcast
113- return if ::Rails . gem_version < Gem ::Version . new ( '7.1' )
114- return unless ::Rails . logger
115-
116- ::Rails . logger . define_singleton_method ( :broadcast_to ) { |*| nil }
117- end
118-
119118 def blacklisted_subscribers
120119 ActiveSupport ::LogSubscriber . log_subscribers . select do |subscriber |
121120 SUBSCRIBER_BLACKLIST . include? ( subscriber . class )
0 commit comments